使用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