I'm trying to group up the name key values here as a key for a dict value, and count the source value as a key for said parent key, and have the count value with as its value.
data = [
{'name':'Gill', 'source':'foo'},
{'name':'Gill', 'source':'foo'},
{'name':'Gill', 'source':'foo'},
{'name':'Gill', 'source':'bar'},
{'name':'Gill', 'source':'bar'},
{'name':'Gill', 'source':'bar'},
{'name':'Gill', 'source':'bar'},
{'name':'Gill', 'source':'bar'},
{'name':'Dave', 'source':'foo'},
{'name':'Dave', 'source':'foo'},
{'name':'Dave', 'source':'foo'},
{'name':'Dave', 'source':'foo'},
{'name':'Dave', 'source':'egg'},
{'name':'Dave', 'source':'egg'},
{'name':'Dave', 'source':'egg'},
{'name':'Dave', 'source':'egg'},
{'name':'Dave', 'source':'egg'},
{'name':'Dave', 'source':'egg'},
{'name':'Dave', 'source':'egg'}
]
How do I achieve the below output?
{'Gill': {'foo':3, 'bar':5}, 'Dave': {'foo':4, 'egg':7}}
I think it may be possible with a 1 liner...
Use itertools.groupby to group by names, then collections.Counter to count the source categories belonging to each name:
from collections import Counter
from itertools import groupby
f = lambda x: x['name']
dct = {k: Counter(d['source'] for d in g) for k, g in groupby(data, f)}
print(dct)
# {'Gill': Counter({'bar': 5, 'foo': 3}), 'Dave': Counter({'egg': 7, 'foo': 4})}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With