具有列表形式的多个值的熊猫 value_counts()?

use*_*200 4 python dataframe pandas

我正在尝试为数据框中的特定列执行 value_count

例如:

    <Fruit>
0   'apple'
1   'apple, orange'
2   'orange'
Run Code Online (Sandbox Code Playgroud)

我如何总结它以便即使它在列表中也会计算它?所以上面应该给我:

'Apple'   2
'Orange'  2
Run Code Online (Sandbox Code Playgroud)

我尝试将字符串转换为列表,但不确定如何对具有值列表的字段进行 value_count。

Jef*_*eff 5

这是一种潘多尼的方式

In [8]: s
Out[8]: 
0            apple
1    apple, orange
2           orange
dtype: object
Run Code Online (Sandbox Code Playgroud)

按分隔符拆分字符串,将它们转换为系列并计算它们。

In [9]: s.str.split(',\s+').apply(lambda x: Series(x).value_counts()).sum()
Out[9]: 
apple     2
orange    2
dtype: float64
Run Code Online (Sandbox Code Playgroud)