[英]Count frequency of values in subgroup pandas
I am trying to count the number of True and False values in a pandas df like this.我正在尝试像这样计算 pandas df 中 True 和 False 值的数量。 However, I'm having trouble first using groupby() to isolate my groups before using count().但是,我在使用 count() 之前首先使用 groupby() 来隔离我的组时遇到了麻烦。
Here's a toy example of my df in which A, B, and C are columns:这是我的 df 的一个玩具示例,其中 A、B 和 C 是列:
A B C
1 a true
1 a true
1 b true
1 b false
2 c false
2 c true
2 c true
2 d false
Does anyone have help?有人有帮助吗? Here's my desired output:这是我想要的 output:
B true false
a 2 0
b 1 1
c 1 2
d 0 1
I'm new to coding in python, so many thanks in advance!我是 python 编码的新手,提前非常感谢!
We can try crosstab
我们可以试试crosstab
pd.crosstab(df.B,df.C)
C False True
B
a 0 2
b 1 1
c 1 2
d 1 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.