答案 0 :(得分:2)
对于0.25+的熊猫,请使用Series.explode
:
s = df['Tags'].explode().value_counts()
使用DataFrame构造函数和DataFrame.stack
的另一种解决方案也适用于0.25
下的版本:
s = pd.DataFrame(df['Tags'].tolist()).stack().value_counts()
或者可以将纯python与Counter
并进行展平:
from collections import Counter
s = pd.Series(Counter([y for x in df['Tags'] for y in x]))
示例:
df = pd.DataFrame({'Tags':[['a','b'],['a','b','c'],['c','b','c'], ['c']]})
s = df['Tags'].explode().value_counts()
print(s)
c 4
b 3
a 2
Name: Tags, dtype: int64