繁体   English   中英

pandas:计算列表中每个元素在列表列中唯一出现的次数

[英]pandas: count the number of unique occurrences of each element of list in a column of lists

我有一个包含一列列表的 dataframe 如下:

df
     pos_tag
0    ['Noun','verb','adjective']
1    ['Noun','verb']
2    ['verb','adjective']
3    ['Noun','adverb']
...

我想得到的是每个唯一元素作为字典出现在整个列中的次数:

desired output:
my_dict = {'Noun':3, 'verb':3, 'adjective':2, 'adverb':1}

使用Series.explode以及Series.value_countsSeries.to_dict

freq = df['pos_tag'].explode().value_counts().to_dict()

结果:

# print(freq)
{'Noun':3, 'verb':3, 'adjective':2, 'adverb':1}

为了提高性能,请使用带有嵌套列表扁平值的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}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM