计算pandas数据帧中文本的出现次数

时间:2016-11-28 05:03:29

标签: python pandas

我的任务是使用pandas来检查我的csv文件中出现的文本的出现次数。但是我对熊猫图书馆很困惑,如果有人可以帮助我,我会很感激。

//例

//title row [ round 1, round 2, round 3]

//row 1 [ 1, 2, 0]

//row 2 [ 2, 2, 0]

//row 3 [ 0, 1, 1]

所以我需要输出

出现次数:

0 = 3
1 = 2
2 = 3

知道我该怎么办吗?

2 个答案:

答案 0 :(得分:0)

value_counts在系列赛中计算此结果。首先需要通过将所有列堆叠成一个来获得该系列:

df.stack()
Out[14]: 
0  round1     1
    round2    2
    round3    0
1  round1     2
    round2    2
    round3    0
2  round1     0
    round2    1
    round3    1
dtype: int64

df.stack().value_counts()
Out[15]: 
2    3
1    3
0    3
dtype: int64

答案 1 :(得分:0)

使用pandas.read_csv导入.csv文件。然后,您可以使用pandas.value_counts

循环列