Python 为列创建类别计数字典
我有以下数据框:Python 为列创建类别计数字典,python,python-3.x,pandas,dictionary,counter,Python,Python 3.x,Pandas,Dictionary,Counter,我有以下数据框: person pets John [cat, dog] Amy [bird, fish, cat] Dave [cow, horse, dog] Mary [lamb, camel, rino] Jim [bird, dog] 我想聚合pets列,以查找每种宠物类型的出现次数。本例的预期答案应为: {cat: 2, dog: 3, bird:2, fish:1, cow:1, horse:1, lamb: 1, camel:
person pets
John [cat, dog]
Amy [bird, fish, cat]
Dave [cow, horse, dog]
Mary [lamb, camel, rino]
Jim [bird, dog]
我想聚合pets
列,以查找每种宠物类型的出现次数。本例的预期答案应为:
{cat: 2, dog: 3, bird:2, fish:1, cow:1, horse:1, lamb: 1, camel: 1, rino:1}
除了逐行循环整个数据帧外,还有更优雅的方法获得结果吗?谢谢 是的,您可以使用计数器
import collections
import pandas
d = {'person': ['John', 'Amy', 'Dave', 'Mary','Jim'], 'pets': [['cat','dog'], ['bird','fish','cat'],['cow','horse','dog'], ['lamb', 'camel' , 'rhino'],['bird','dog']]}
df1 = pd.DataFrame.from_dict(d)
collections.Counter(sum(df1.pets,[]))
并以良好的格式输出
counts = pd.DataFrame.from_dict(collections.Counter(sum(df1.pets,[])),orient='index')
输出:
0
cat 2
dog 3
bird 2
fish 1
cow 1
horse 1
lamb 1
camel 1
rhino 1
用于:
通过使用内置功能,您可以:
a = [j for i in df['pets'] for j in i]
{i:a.count(i) for i in set(a)}
{'fish': 1,'bird': 2,'dog': 3,'camel': 1,'cat': 2,'lamb': 1,'horse': 1,'cow': 1,'rhino': 1}
您是否可以选择以不同方式构建数据帧,同时保留相同的数据?
a = [j for i in df['pets'] for j in i]
{i:a.count(i) for i in set(a)}
{'fish': 1,'bird': 2,'dog': 3,'camel': 1,'cat': 2,'lamb': 1,'horse': 1,'cow': 1,'rhino': 1}