'Using Values in dictionary to groupby and average

new_list_df = list(map(lambda x : list(range(x[0], x[1]+1)), df_test[['StartMonth','EndMonth']].values))

display(new_list_df)

output:

[[7, 8, 9, 10, 11, 12]]

my_dict = dict(zip(cheat_list, new_list_df))
print(my_dict)

Output: {'2019PeakWE': [7, 8, 9, 10, 11, 12]}

I would like to use mydict output and average all of the months on the value part of the key. Is that possible? The months and values I would like it to display is in the link.



Solution 1:[1]

By dict comprehension you can do

example = {"2019PeakWE": [7, 8, 9, 10, 11, 12], "tutu": [1, 3]}

averages = {key: sum(example[key]) / len(example[key]) for key in example}
print(averages)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Floh