Home > Back-end >  Get the total value per month in Python Dictionary where Key is DateTime object and Value is list of
Get the total value per month in Python Dictionary where Key is DateTime object and Value is list of

Time:12-19

I have a Python Dictionary where my key is a DateTime object and my Values are a list of numbers.

It looks like this:

enter image description here

I'm trying to get the total values per month based on the key, so if the key includes "09" as month then September will have 6 values.

I'm relatively new to Python and unsure on what would be the best approach for this, so far I have come up with something like this but I think I'm approaching it wrong:

if dictionary.keys() == re.search([0-9]{4}-(09)-[0-9]{2}):
    september.append(dictionary.value())
    total = len(september)
elif dictionary.keys() == re.search([0-9]{4}-(10)-[0-9]{2}):
    october.append(dictionary.value())
    total = len(october)

The dictionary:

{datetime.date(2020, 9, 16): [20412], datetime.date(2020, 9, 23): [20424], datetime.date(2020, 9, 30): [20427, 20431, 20428], datetime.date(2020, 10, 6): [20435], datetime.date(2020, 10, 8): [20447], datetime.date(2020, 10, 7): [20448, 20444, 20442, 20441], datetime.date(2020, 10, 5): [20434, 20436], datetime.date(2020, 10, 13): [20454, 20453], datetime.date(2020, 10, 14): [20459, 20463, 20462], datetime.date(2020, 10, 19): [20467, 20470, 20465], datetime.date(2020, 9, 28): [20429], datetime.date(2020, 10, 12): [20450], datetime.date(2020, 10, 28): [20478, 20480, 20479], datetime.date(2020, 10, 27): [20481], datetime.date(2020, 10, 20): [20468], datetime.date(2020, 11, 3): [20490, 20485, 20486], datetime.date(2020, 11, 5): [20491], datetime.date(2020, 11, 4): [20487], datetime.date(2020, 11, 11): [20494, 20495, 20499], datetime.date(2020, 11, 13): [20502], datetime.date(2020, 11, 12): [20500], datetime.date(2020, 11, 25): [20522, 20533, 20535, 20526, 20528, 20523], datetime.date(2020, 11, 19): [20514], datetime.date(2020, 11, 16): [20505], datetime.date(2020, 11, 18): [20508, 20519], datetime.date(2020, 12, 9): [20560, 20559], datetime.date(2020, 12, 11): [20562, 20561], datetime.date(2020, 12, 3): [20554, 20553, 20552], datetime.date(2020, 12, 2): [20542, 20546, 20551], datetime.date(2020, 11, 27): [20534], datetime.date(2020, 11, 23): [20525, 20524], datetime.date(2020, 11, 17): [20507], datetime.date(2020, 12, 16): [20569], datetime.date(2020, 12, 22): [20594, 20591, 20592, 20600, 20612, 20589], datetime.date(2021, 1, 6): [215, 214], datetime.date(2020, 12, 14): [20567], datetime.date(2020, 12, 23): [20606, 20604, 20607, 20585], datetime.date(2020, 12, 21): [20587], datetime.date(2021, 1, 21): [2110, 2114], datetime.date(2021, 1, 20): [2116], datetime.date(2021, 1, 27): [2133, 2125, 2126, 2129, 2134, 2130, 2122], datetime.date(2021, 1, 26): [2128], datetime.date(2021, 2, 8): [2150], datetime.date(2021, 2, 3): [2142, 2146], datetime.date(2021, 2, 17): [2170, 2164, 2166], datetime.date(2021, 2, 11): [2156, 2157], datetime.date(2021, 2, 5): [2147], datetime.date(2021, 1, 28): [2132], datetime.date(2021, 2, 15): [2160, 2161], datetime.date(2021, 1, 14): [217], datetime.date(2021, 2, 10): [2154], datetime.date(2021, 2, 18): [2174], datetime.date(2021, 3, 3): [21121, 21120, 21119, 21112], datetime.date(2021, 2, 24): [2193], datetime.date(2021, 2, 19): [2184], datetime.date(2021, 2, 25): [2199], datetime.date(2021, 3, 1): [21103], datetime.date(2021, 3, 10): [21130, 21138, 21142, 21136, 21137], datetime.date(2021, 3, 12): [21135, 21134], datetime.date(2021, 3, 18): [21156, 21145], datetime.date(2021, 3, 2): [21105], datetime.date(2021, 3, 16): [21144, 21163, 21154, 21155, 21158], datetime.date(2021, 3, 15): [21150, 21148], datetime.date(2021, 3, 19): [21159, 21161, 21160], datetime.date(2021, 3, 30): [21184, 21179], datetime.date(2021, 3, 31): [21182, 21187, 21188], datetime.date(2021, 3, 29): [21177], datetime.date(2021, 3, 26): [21174], datetime.date(2021, 3, 24): [21166, 21169], datetime.date(2021, 3, 22): [21164], datetime.date(2021, 4, 1): [21189], datetime.date(2021, 4, 7): [21200, 21203, 21195, 21198, 21193, 21202], datetime.date(2021, 3, 9): [21127], datetime.date(2021, 4, 12): [21209, 21207, 21210], datetime.date(2021, 4, 23): [21234, 21233], datetime.date(2021, 4, 21): [21231, 21230, 21232], datetime.date(2021, 4, 19): [21224, 21221, 21219], datetime.date(2021, 4, 20): [21227], datetime.date(2021, 4, 14): [21214, 21212, 21217, 21194, 21211, 21215], datetime.date(2021, 4, 28): [21240, 21243, 21245], datetime.date(2021, 4, 27): [21241, 21242], datetime.date(2021, 4, 29): [21250, 21247], datetime.date(2021, 4, 30): [21248], datetime.date(2021, 5, 5): [21254, 21253, 21255, 21259], datetime.date(2021, 5, 7): [21262], datetime.date(2021, 4, 6): [21197], datetime.date(2021, 5, 10): [21266, 21267], datetime.date(2021, 5, 12): [21268, 21269, 21271, 21273, 21277, 21278, 21275], datetime.date(2021, 5, 17): [21287, 21282, 21294], datetime.date(2021, 5, 19): [21289, 21292, 21293, 21290, 21283], datetime.date(2021, 5, 24): [21306, 21314, 21311, 21297, 21308], datetime.date(2021, 5, 25): [21300, 21300], datetime.date(2021, 5, 26): [21315, 21313, 21312, 21319, 21316, 21301, 21302, 21307], datetime.date(2021, 5, 27): [21320], datetime.date(2021, 6, 2): [21326, 21327, 21328, 21329, 21330], datetime.date(2021, 6, 1): [21323], datetime.date(2021, 6, 3): [21332, 21331, 21340], datetime.date(2021, 6, 9): [21347, 21341, 21344, 21348, 21349, 21355], datetime.date(2021, 6, 4): [21335, 21336, 21338, 21339], datetime.date(2021, 6, 10): [21352], datetime.date(2021, 6, 16): [21362, 21364, 21365], datetime.date(2021, 6, 15): [21371], datetime.date(2021, 6, 14): [21372], datetime.date(2021, 6, 21): [21380, 21385], datetime.date(2021, 6, 25): [21389], datetime.date(2021, 6, 30): [21406, 21408, 21411], datetime.date(2021, 6, 28): [21393, 21392], datetime.date(2021, 7, 1): [21391, 21412, 21413, 21409], datetime.date(2021, 6, 29): [21399, 21400], datetime.date(2021, 7, 7): [21423, 21426, 21424, 21427], datetime.date(2021, 7, 6): [21420, 21416], datetime.date(2021, 7, 5): [21417], datetime.date(2021, 7, 12): [21431, 21432], datetime.date(2021, 7, 14): [21438, 21429, 21442, 21446, 21445], datetime.date(2021, 7, 13): [21434], datetime.date(2021, 7, 21): [21451], datetime.date(2021, 7, 22): [21452, 21462], datetime.date(2021, 7, 19): [21464], datetime.date(2021, 7, 28): [21478, 21472, 21483, 21469, 21468, 21473, 21473, 21475], datetime.date(2021, 7, 29): [21480], datetime.date(2021, 8, 10): [21499, 21497], datetime.date(2021, 8, 13): [21506], datetime.date(2021, 8, 12): [21502], datetime.date(2021, 8, 9): [21500, 21498], datetime.date(2021, 8, 4): [21494, 21490, 21486], datetime.date(2021, 8, 11): [21505], datetime.date(2021, 8, 3): [21484], datetime.date(2021, 8, 6): [21488], datetime.date(2021, 8, 27): [21530, 21532, 21538, 21540], datetime.date(2021, 8, 20): [21524], datetime.date(2021, 8, 16): [21519], datetime.date(2021, 8, 24): [21533], datetime.date(2021, 8, 25): [21539, 21536], datetime.date(2021, 9, 1): [21550, 21549, 21545, 21547, 21541, 21543], datetime.date(2021, 8, 30): [21546], datetime.date(2021, 9, 8): [21554, 21555], datetime.date(2021, 9, 10): [21559]}

CodePudding user response:

I believe you are looking for the below (the idea is to extract the month from the date and use a defaultdict is order to map the values of each month)

from collections import defaultdict
import datetime 

data = {datetime.date(2020, 9, 16): [20412], datetime.date(2020, 9, 23): [20424], datetime.date(2020, 9, 30): [20427, 20431, 20428], datetime.date(2020, 10, 6): [20435], datetime.date(2020, 10, 8): [20447], datetime.date(2020, 10, 7): [20448, 20444, 20442, 20441], datetime.date(2020, 10, 5): [20434, 20436], datetime.date(2020, 10, 13): [20454, 20453], datetime.date(2020, 10, 14): [20459, 20463, 20462], datetime.date(2020, 10, 19): [20467, 20470, 20465], datetime.date(2020, 9, 28): [20429], datetime.date(2020, 10, 12): [20450], datetime.date(2020, 10, 28): [20478, 20480, 20479], datetime.date(2020, 10, 27): [20481], datetime.date(2020, 10, 20): [20468], datetime.date(2020, 11, 3): [20490, 20485, 20486], datetime.date(2020, 11, 5): [20491], datetime.date(2020, 11, 4): [20487], datetime.date(2020, 11, 11): [20494, 20495, 20499], datetime.date(2020, 11, 13): [20502], datetime.date(2020, 11, 12): [20500], datetime.date(2020, 11, 25): [20522, 20533, 20535, 20526, 20528, 20523], datetime.date(2020, 11, 19): [20514], datetime.date(2020, 11, 16): [20505], datetime.date(2020, 11, 18): [20508, 20519], datetime.date(2020, 12, 9): [20560, 20559], datetime.date(2020, 12, 11): [20562, 20561], datetime.date(2020, 12, 3): [20554, 20553, 20552], datetime.date(2020, 12, 2): [20542, 20546, 20551], datetime.date(2020, 11, 27): [20534], datetime.date(2020, 11, 23): [20525, 20524], datetime.date(2020, 11, 17): [20507], datetime.date(2020, 12, 16): [20569], datetime.date(2020, 12, 22): [20594, 20591, 20592, 20600, 20612, 20589], datetime.date(2021, 1, 6): [215, 214], datetime.date(2020, 12, 14): [20567], datetime.date(2020, 12, 23): [20606, 20604, 20607, 20585], datetime.date(2020, 12, 21): [20587], datetime.date(2021, 1, 21): [2110, 2114], datetime.date(2021, 1, 20): [2116], datetime.date(2021, 1, 27): [2133, 2125, 2126, 2129, 2134, 2130, 2122], datetime.date(2021, 1, 26): [2128], datetime.date(2021, 2, 8): [2150], datetime.date(2021, 2, 3): [2142, 2146], datetime.date(2021, 2, 17): [2170, 2164, 2166], datetime.date(2021, 2, 11): [2156, 2157], datetime.date(2021, 2, 5): [2147], datetime.date(2021, 1, 28): [2132], datetime.date(2021, 2, 15): [2160, 2161], datetime.date(2021, 1, 14): [217], datetime.date(2021, 2, 10): [2154], datetime.date(2021, 2, 18): [2174], datetime.date(2021, 3, 3): [21121, 21120, 21119, 21112], datetime.date(2021, 2, 24): [2193], datetime.date(2021, 2, 19): [2184], datetime.date(2021, 2, 25): [2199], datetime.date(2021, 3, 1): [21103], datetime.date(2021, 3, 10): [21130, 21138, 21142, 21136, 21137], datetime.date(2021, 3, 12): [21135, 21134], datetime.date(2021, 3, 18): [21156, 21145], datetime.date(2021, 3, 2): [21105], datetime.date(2021, 3, 16): [21144, 21163, 21154, 21155, 21158], datetime.date(2021, 3, 15): [21150, 21148], datetime.date(2021, 3, 19): [21159, 21161, 21160], datetime.date(2021, 3, 30): [21184, 21179], datetime.date(2021, 3, 31): [21182, 21187, 21188], datetime.date(2021, 3, 29): [21177], datetime.date(2021, 3, 26): [21174], datetime.date(2021, 3, 24): [21166, 21169], datetime.date(2021, 3, 22): [21164], datetime.date(2021, 4, 1): [21189], datetime.date(2021, 4, 7): [21200, 21203, 21195, 21198, 21193, 21202], datetime.date(2021, 3, 9): [21127], datetime.date(2021, 4, 12): [21209, 21207, 21210], datetime.date(2021, 4, 23): [21234, 21233], datetime.date(2021, 4, 21): [21231, 21230, 21232], datetime.date(2021, 4, 19): [21224, 21221, 21219], datetime.date(2021, 4, 20): [21227], datetime.date(2021, 4, 14): [21214, 21212, 21217, 21194, 21211, 21215], datetime.date(2021, 4, 28): [21240, 21243, 21245], datetime.date(2021, 4, 27): [21241, 21242], datetime.date(2021, 4, 29): [21250, 21247], datetime.date(2021, 4, 30): [21248], datetime.date(2021, 5, 5): [21254, 21253, 21255, 21259], datetime.date(2021, 5, 7): [21262], datetime.date(2021, 4, 6): [21197], datetime.date(2021, 5, 10): [21266, 21267], datetime.date(2021, 5, 12): [21268, 21269, 21271, 21273, 21277, 21278, 21275], datetime.date(2021, 5, 17): [21287, 21282, 21294], datetime.date(2021, 5, 19): [21289, 21292, 21293, 21290, 21283], datetime.date(2021, 5, 24): [21306, 21314, 21311, 21297, 21308], datetime.date(2021, 5, 25): [21300, 21300], datetime.date(2021, 5, 26): [21315, 21313, 21312, 21319, 21316, 21301, 21302, 21307], datetime.date(2021, 5, 27): [21320], datetime.date(2021, 6, 2): [21326, 21327, 21328, 21329, 21330], datetime.date(2021, 6, 1): [21323], datetime.date(2021, 6, 3): [21332, 21331, 21340], datetime.date(2021, 6, 9): [21347, 21341, 21344, 21348, 21349, 21355], datetime.date(2021, 6, 4): [21335, 21336, 21338, 21339], datetime.date(2021, 6, 10): [21352], datetime.date(2021, 6, 16): [21362, 21364, 21365], datetime.date(2021, 6, 15): [21371], datetime.date(2021, 6, 14): [21372], datetime.date(2021, 6, 21): [21380, 21385], datetime.date(2021, 6, 25): [21389], datetime.date(2021, 6, 30): [21406, 21408, 21411], datetime.date(2021, 6, 28): [21393, 21392], datetime.date(2021, 7, 1): [21391, 21412, 21413, 21409], datetime.date(2021, 6, 29): [21399, 21400], datetime.date(2021, 7, 7): [21423, 21426, 21424, 21427], datetime.date(2021, 7, 6): [21420, 21416], datetime.date(2021, 7, 5): [21417], datetime.date(2021, 7, 12): [21431, 21432], datetime.date(2021, 7, 14): [21438, 21429, 21442, 21446, 21445], datetime.date(2021, 7, 13): [21434], datetime.date(2021, 7, 21): [21451], datetime.date(2021, 7, 22): [21452, 21462], datetime.date(2021, 7, 19): [21464], datetime.date(2021, 7, 28): [21478, 21472, 21483, 21469, 21468, 21473, 21473, 21475], datetime.date(2021, 7, 29): [21480], datetime.date(2021, 8, 10): [21499, 21497], datetime.date(2021, 8, 13): [21506], datetime.date(2021, 8, 12): [21502], datetime.date(2021, 8, 9): [21500, 21498], datetime.date(2021, 8, 4): [21494, 21490, 21486], datetime.date(2021, 8, 11): [21505], datetime.date(2021, 8, 3): [21484], datetime.date(2021, 8, 6): [21488], datetime.date(2021, 8, 27): [21530, 21532, 21538, 21540], datetime.date(2021, 8, 20): [21524], datetime.date(2021, 8, 16): [21519], datetime.date(2021, 8, 24): [21533], datetime.date(2021, 8, 25): [21539, 21536], datetime.date(2021, 9, 1): [21550, 21549, 21545, 21547, 21541, 21543], datetime.date(2021, 8, 30): [21546], datetime.date(2021, 9, 8): [21554, 21555], datetime.date(2021, 9, 10): [21559]}

result = defaultdict(list)
for k,v in data.items():
  result[k.month].extend(v)
for month,values in result.items():
  print(f'{month} --> {values}') # if sum is needed - just do sum(values) , if count is needed do len(values)

output

9 --> [20412, 20424, 20427, 20431, 20428, 20429, 21550, 21549, 21545, 21547, 21541, 21543, 21554, 21555, 21559]
10 --> [20435, 20447, 20448, 20444, 20442, 20441, 20434, 20436, 20454, 20453, 20459, 20463, 20462, 20467, 20470, 20465, 20450, 20478, 20480, 20479, 20481, 20468]
11 --> [20490, 20485, 20486, 20491, 20487, 20494, 20495, 20499, 20502, 20500, 20522, 20533, 20535, 20526, 20528, 20523, 20514, 20505, 20508, 20519, 20534, 20525, 20524, 20507]
12 --> [20560, 20559, 20562, 20561, 20554, 20553, 20552, 20542, 20546, 20551, 20569, 20594, 20591, 20592, 20600, 20612, 20589, 20567, 20606, 20604, 20607, 20585, 20587]
1 --> [215, 214, 2110, 2114, 2116, 2133, 2125, 2126, 2129, 2134, 2130, 2122, 2128, 2132, 217]
2 --> [2150, 2142, 2146, 2170, 2164, 2166, 2156, 2157, 2147, 2160, 2161, 2154, 2174, 2193, 2184, 2199]
3 --> [21121, 21120, 21119, 21112, 21103, 21130, 21138, 21142, 21136, 21137, 21135, 21134, 21156, 21145, 21105, 21144, 21163, 21154, 21155, 21158, 21150, 21148, 21159, 21161, 21160, 21184, 21179, 21182, 21187, 21188, 21177, 21174, 21166, 21169, 21164, 21127]
4 --> [21189, 21200, 21203, 21195, 21198, 21193, 21202, 21209, 21207, 21210, 21234, 21233, 21231, 21230, 21232, 21224, 21221, 21219, 21227, 21214, 21212, 21217, 21194, 21211, 21215, 21240, 21243, 21245, 21241, 21242, 21250, 21247, 21248, 21197]
5 --> [21254, 21253, 21255, 21259, 21262, 21266, 21267, 21268, 21269, 21271, 21273, 21277, 21278, 21275, 21287, 21282, 21294, 21289, 21292, 21293, 21290, 21283, 21306, 21314, 21311, 21297, 21308, 21300, 21300, 21315, 21313, 21312, 21319, 21316, 21301, 21302, 21307, 21320]
6 --> [21326, 21327, 21328, 21329, 21330, 21323, 21332, 21331, 21340, 21347, 21341, 21344, 21348, 21349, 21355, 21335, 21336, 21338, 21339, 21352, 21362, 21364, 21365, 21371, 21372, 21380, 21385, 21389, 21406, 21408, 21411, 21393, 21392, 21399, 21400]
7 --> [21391, 21412, 21413, 21409, 21423, 21426, 21424, 21427, 21420, 21416, 21417, 21431, 21432, 21438, 21429, 21442, 21446, 21445, 21434, 21451, 21452, 21462, 21464, 21478, 21472, 21483, 21469, 21468, 21473, 21473, 21475, 21480]
8 --> [21499, 21497, 21506, 21502, 21500, 21498, 21494, 21490, 21486, 21505, 21484, 21488, 21530, 21532, 21538, 21540, 21524, 21519, 21533, 21539, 21536, 21546]

CodePudding user response:

Your dictionary keys are already datetime.date objects. You don't need a regex to parse them. You can directly access their month and year attributes (see https://docs.python.org/3/library/datetime.html#date-objects).

A straightforward approach would be:

  • Prepare a new dictionary result that will map pairs (year, month) to the sums of the corresponding values (include the year to avoid that the values from two different years are mixed together, if that's not what you want)
  • Iterate over all key/value pairs (the value being the list of numbers) in the input dictionary
  • For each key, take the year and month
  • Add the sum of the list of numbers to the value in the result dictionary

The step "prepare a new dictionary that will contain sums" can be done by using a defaultdict (see https://docs.python.org/3/library/collections.html#collections.defaultdict, and How does collections.defaultdict work? for more info).

from collections import defaultdict

result = defaultdict(int)
for date, values in dictionary.items():
    result[date.year, date.month]  = sum(values)

CodePudding user response:

Note that even though you said, if you 9 should have 6 as a value, that's only for 2020 because there is a 2021.09 too. So it seems right to make the output dictionary keys year-month pairs.

Then are a couple of ways. If you want to get the number of values for each year-month pair, you can use dict.get method:

out = {}
for k,v in dct.items():
    out[(k.year, k.month)] = out.get((k.year, k.month), 0)   len(v)

or dict.setdefault method:

out = {}
for k,v in dct.items():
    out.setdefault((k.year, k.month), 0)
    out[(k.year, k.month)]  = len(v)

Output:

>>> out[(2020, 9)]

6

But if you want to get all items in each year-month pair, you can use the same methods by slightly tweaking.

out = {}
for k,v in dct.items():
    out[(k.year, k.month)] = out.get((k.year, k.month), [])   v

or dict.setdefault method:

out = {}
for k,v in dct.items():
    out.setdefault((k.year, k.month), []).extend(v)

Then output:

>>> out[(2020, 9)]

[20412, 20424, 20427, 20431, 20428, 20429]
  • Related