使用Pandas数据框按列对组值进行计数 [英] Group Value Count By Column with Pandas Dataframe

查看:43
本文介绍了使用Pandas数据框按列对组值进行计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太确定该怎么问,因此如果这是重复的问题,我深表歉意.我有一个看起来像这样的数据框:

I'm not really sure how to ask this, so I apologize if this is a repeat question. I have this data frame that looks something like this:

|ID |Attend_x |Attend_y |Attend_z ||1 |没有没有没有|2 |没有没有是的|3 |没有是的没有|4 |没有是的是|

我一直在尝试找出group_by的正确组合,并计数以使其看起来像这样:

I've been trying to figure out the right combination of group_by and count to get it to look like this:

||是的没有| Attend_x |0 |4 || Attend_y |2 |2 || Attend_z |2 |2 |

老实说,我很沮丧.因此,任何建议都将受到高度赞赏.谢谢!

I'm honestly stumped. So any advice is super appreciated. Thanks!

推荐答案

value_counts

df.iloc[:,1:].apply(pd.Series.value_counts).fillna(0).T
Out[184]: 
           No  Yes
Attend_x  4.0  0.0
Attend_y  2.0  2.0
Attend_z  2.0  2.0

或在融化

m = df.iloc[:,1:].melt()
pd.crosstab(m.variable, m.value)

value     No  Yes
variable         
Attend_x   4    0
Attend_y   2    2
Attend_z   2    2

这篇关于使用Pandas数据框按列对组值进行计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆