我有DataFrame
之类的人。
1 A B D E None
2 B C E None None
3 A B D E None
4 A B C E None
5 A B C D E
我想计算DataFrame
中的每个项目。怎么做?
B(6), E(5), A(4), C(4), D(4) etc.
答案 0 :(得分:1)
stack,然后使用value_counts:
import pandas as pd
from StringIO import StringIO
data = \
"""A,B,D,E,None
B,C,E,None,None
A,B,D,E,None
A,B,C,E,None
A,B,C,D,E"""
# Creation of the dataframe
df = pd.read_csv(StringIO(data),sep = ',',header=None)
# Perform the magic operation (stack and value_counts) on the dataframe
res = df.stack().value_counts()
结果如下:
E 5
B 5
None 5
A 4
C 3
D 3
dtype: int64