我有一个包含一列列表的数据框,如下所示:df pos_tag0 ['Noun','verb','adjective']1 ['Noun','verb']2 ['verb','adjective']3 ['Noun','adverb']...我想得到的是每个唯一元素作为字典出现在整个列中的次数:desired output:my_dict = {'Noun':3, 'verb':3, 'adjective':2, 'adverb':1}
2 回答
慕运维8079593
TA贡献1876条经验 获得超5个赞
与和Series.explode
一起使用:Series.value_counts
Series.to_dict
freq = df['pos_tag'].explode().value_counts().to_dict()
结果:
# print(freq) {'Noun':3, 'verb':3, 'adjective':2, 'adverb':1}
ABOUTYOU
TA贡献1812条经验 获得超5个赞
为了提高性能,请使用Counter嵌套列表的展平值:
from collections import Counter
my_dict = dict(Counter([y for x in df['pos_tag'] for y in x]))
print (my_dict)
{'Noun': 3, 'verb': 3, 'adjective': 2, 'adverb': 1}
添加回答
举报
0/150
提交
取消