在 pandas 数据框中按列计数出现的次数 [英] count occurrences of number by column in pandas data frame
本文介绍了在 pandas 数据框中按列计数出现的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个熊猫数据框,我想统计数字在每一列中出现的频率
I have a pandas data frame I want to count how often a number appears in a column for each column
a b c d e
0 2 3 1 5 4
1 1 3 2 5 4
2 1 3 2 5 4
3 2 4 1 5 3
4 2 4 1 5 3
这是我的无效代码
def equalsOne(x):
x[x.columns == 1].sum()
df1.apply(equalOne(), axis = 1)
这是所需的输出
a 2
b 0
c 3
d 0
e 0
推荐答案
您可以这样做:
(df==1).sum()
df==1
给出:
a b c d e
0 False False True False False
1 True False False False False
2 True False False False False
3 False False True False False
4 False False True False False
和sum()
将False
视为0
,将True
视为1
.
这篇关于在 pandas 数据框中按列计数出现的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文