Get the average of dictionary values? [closed]
This answer was intended for Python2, which is now dead
Okay, so let's iterate over all dictionary keys and average the items:
avgDict = {}
for k,v in StudentGrades.iteritems():
# v is the list of grades for student k
avgDict[k] = sum(v)/ float(len(v))
In Python3, the
iteritems()
method is no longer necessary, can useitems()
directly.
now you can just see :
avgDict
Out[5]:
{'Ivan': 3.106666666666667,
'Martin': 4.816666666666666,
'Stoyan': 3.89,
'Vladimir': 5.433333333333334}
From your question I think you're queasy about iteration over dicts, so here is the same with output as a list :
avgList = []
for k,v in StudentGrades.iteritems():
# v is the list of grades for student k
avgDict.append(sum(v)/ float(len(v)))
Be careful though : the order of items in a dictionary is NOT guaranteed; this is, the order of key/values when printing or iterating on the dictionary is not guaranteed (as dicts are "unsorted").
Looping over the same identical dictionary object(with no additions/removals) twice is guaranteed to behave identically though.
How to calculate an average from average values in a dictionary
You can take the values of your newly created dictionary using .values
and then apply the same logic you applied earlier to get an average of averages.
def avg(classes):
average = {}
for classnames, grades in classes.items():
average[classnames] = sum(grades) / len(grades)
return average
classes = {"Spanish II": [100, 99, 100, 98], "US History I": [95, 96, 97, 94]}
averages = avg(classes)
#{'Spanish II': 99.25, 'US History I': 95.5}
average_of_averages = sum(averages.values())/len(averages)
#97.375
Get average value from list of dictionary
Just divide the sum of values by the length of the list:
print sum(d['value'] for d in total) / len(total)
Note that division of integers returns the integer value. This means that average of the [5, 5, 0, 0]
will be 2
instead of 2.5
. If you need more precise result then you can use the float()
value:
print float(sum(d['value'] for d in total)) / len(total)
How to calculate the mean of elements of a lists inside a dictionary in python?
First: your example is not correct dictionary. You missed {}
in some places.
You should have
{
'NN3-001': {'diffe_1':[1,2,3,4],'mas_1':[10,20,30,40],'diffe_2':[5,6,7,8],'mas_2':[50,60,70,80]},
'NN3-002': {'diffe_1':[14,15,16,17],'mas_1':[100,200,300,400],'diffe_2':[18,19,20,21],'mas_2':[500,600,700,800]}
}
To get single list you can use
values = data['NN3-001']['diffe_1']
and you can calculate mean
mean = sum(values)/len(values)
For all list you have to use for
-loops with dict.items()
dictionary = {
'NN3-001': {'diffe_1':[1,2,3,4],'mas_1':[10,20,30,40],'diffe_2':[5,6,7,8],'mas_2':[50,60,70,80]},
'NN3-002': {'diffe_1':[14,15,16,17],'mas_1':[100,200,300,400],'diffe_2':[18,19,20,21],'mas_2':[500,600,700,800]}
}
for name, values in dictionary.items():
print('=== time serie:', name, '===')
for key, data in values.items():
print(' key:', key)
print(' data:', data)
print(' mean:', sum(data)/len(data))
print('---')
Result:
=== time serie: NN3-001 ===
key: diffe_1
data: [1, 2, 3, 4]
mean: 2.5
---
key: mas_1
data: [10, 20, 30, 40]
mean: 25.0
---
key: diffe_2
data: [5, 6, 7, 8]
mean: 6.5
---
key: mas_2
data: [50, 60, 70, 80]
mean: 65.0
---
=== time serie: NN3-002 ===
key: diffe_1
data: [14, 15, 16, 17]
mean: 15.5
---
key: mas_1
data: [100, 200, 300, 400]
mean: 250.0
---
key: diffe_2
data: [18, 19, 20, 21]
mean: 19.5
---
key: mas_2
data: [500, 600, 700, 800]
mean: 650.0
EDIT:
After changes in question I see that you need zip(diffe_1, diffe_2)
to create pairs.
dictionary = {
'NN3-001': {'diffe_1':[1,2,3,4],'mas_1':[10,20,30,40],'diffe_2':[5,6,7,8],'mas_2':[50,60,70,80]},
'NN3-002': {'diffe_1':[14,15,16,17],'mas_1':[100,200,300,400],'diffe_2':[18,19,20,21],'mas_2':[500,600,700,800]}
}
result = {}
for name, values in dictionary.items():
print('=== time serie:', name, '===')
result[name] = {'diff':[], 'mas':[]}
print('--- diffe_1, diffe_2 ---')
for a, b in zip(values['diffe_1'],values['diffe_2']):
mean = int( (a+b)/2 )
print(a, '&', b, '=>', mean)
result[name]['diff'].append(mean)
print('--- mas_1, mas_2 ---')
for a, b in zip(values['mas_1'],values['mas_2']):
mean = int( (a+b)/2 )
print(a, '&', b, '=>', mean)
result[name]['mas'].append(mean)
print(result)
gives
=== time serie: NN3-001 ===
--- diffe_1, diffe_2 ---
1 & 5 => 3.0
2 & 6 => 4.0
3 & 7 => 5.0
4 & 8 => 6.0
--- mas_1, mas_2 ---
10 & 50 => 30.0
20 & 60 => 40.0
30 & 70 => 50.0
40 & 80 => 60.0
=== time serie: NN3-002 ===
--- diffe_1, diffe_2 ---
14 & 18 => 16.0
15 & 19 => 17.0
16 & 20 => 18.0
17 & 21 => 19.0
--- mas_1, mas_2 ---
100 & 500 => 300.0
200 & 600 => 400.0
300 & 700 => 500.0
400 & 800 => 600.0
{
'NN3-001': {'diff': [3, 4, 5, 6], 'mas': [30, 40, 50, 60]},
'NN3-002': {'diff': [16, 17, 18, 19], 'mas': [300, 400, 500, 600]}
}
You may also use loop for prefix in ['diffe', 'mas']:
to reduce code.
dictionary = {
'NN3-001': {'diffe_1':[1,2,3,4],'mas_1':[10,20,30,40],'diffe_2':[5,6,7,8],'mas_2':[50,60,70,80]},
'NN3-002': {'diffe_1':[14,15,16,17],'mas_1':[100,200,300,400],'diffe_2':[18,19,20,21],'mas_2':[500,600,700,800]}
}
result = {}
for name, values in dictionary.items():
print('=== time serie:', name, '===')
result[name] = {}
for prefix in ['diffe', 'mas']:
print('--- prefix:', prefix, '---')
result[name][prefix] = []
for a, b in zip(values[prefix+'_1'],values[prefix+'_2']):
mean = int( (a+b)/2 )
print(a, '&', b, '=>', mean)
result[name][prefix].append(mean)
print(result)
Average of key values in a list of dictionaries
You can use zip
and numpy
functions mean
and round
for this task:
In [8]: import numpy as np
In [9]: [dict(zip(d.keys(), [int(np.round(np.mean(d.values())))])) for d in L]
#Out[9]: [{'Eva': 5}, {'Ana': 53}, {'Ada': 12}]
Version with "less" parenthesis:
[dict(zip(d.keys(), [np.array(d.values()).mean().round().astype(int)])) for d in L]
average list of dictionaries in python
You can use the following
sum([item['Weight'] for item in data])/len(data)
and use
float(len(data))
if you want a more exact value.
Python - Find average in dict elements
You could create an intermediate dictionary that collects all encountered values as lists:
dct = [{'a':2, 'b':3}, {'b':4}, {'a':1, 'c':5}]
from collections import defaultdict
intermediate = defaultdict(list)
for subdict in dct:
for key, value in subdict.items():
intermediate[key].append(value)
# intermediate is now: defaultdict(list, {'a': [2, 1], 'b': [3, 4], 'c': [5]})
And finally calculate the average by dividing the sum of each list by the length of each list:
for key, value in intermediate.items():
print(key, sum(value)/len(value))
which prints:
b 3.5
c 5.0
a 1.5
Average each dictionary list value
Assuming you meant for each value in the dictionary to be a list of numbers, here's a way to do what you're asking:
d = { "numbers1" : [4,5,6,3,2] , "numbers2" : [40,5,63,2] , "numbers3" : [10,25,96,3]}
avg = {k: sum(v) / len(v) for k, v in d.items()}
print(avg)
sortedAvg = sorted(d.items(), key=lambda x: sum(x[1]) / len(x[1]))
print(sortedAvg)
Output:
{'numbers1': 4.0, 'numbers2': 27.5, 'numbers3': 33.5}
[('numbers1', [4, 5, 6, 3, 2]), ('numbers2', [40, 5, 63, 2]), ('numbers3', [10, 25, 96, 3])]
Explanation:
avg
uses a dict comprehension to create a dictionary with the same keys as the input containing the average of each key's listsortedAvg
uses a list comprehension to create a list of tuples of key/value pairs in the input dict sorted in the order of ascending average.
Related Topics
Plot Different Dataframes in the Same Figure
How to Split Text Without Spaces into List of Words
How to Clear or Overwrite a Tkinter Canvas
How to Compile Python Script to Binary Executable
Regex Check If Specific Multiple Words Present in a Sentence
Saving the Output of a Python Program
Python: [Errno 10054] an Existing Connection Was Forcibly Closed by the Remote Host
What Do Numbers Starting With 0 Mean in Python
How to Remove All Characters Before a Specific Character in Python
Convert Commas Decimal Separators to Dots Within a Dataframe
Printing the Number of Days in a Given Month and Year [Python]
Get Only Unique Words from a Sentence in Python
Filter All Rows That Do Not Contain Letters (Alpha) in 'Pandas'
Easiest Way to Ignore Blank Lines When Reading a File in Python
How to Open a Password Protected Excel File Using Python
How to Extract X,Y Coordinates from Opencv "Cv2.Keypoint" Object