pandas 数据框.按值和计数分组 [英] Pandas dataframe. Group by value and count

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

问题描述

我有下表:

Days, Age,  Sex

5,    39,   F
4,    54,   M
4,    26,   M
5,    42,   M
4,    29,   M

我想分别计算F和M的行数.以下命令有效,但是我对表示形式不满意:

I want to count number of rows with F and M separately. The following command works, but I'm not OK with the representation:

df.groupby("Sex").count()

什么是最好的方法?谢谢.

What will be the best way to do it? Thank you.

推荐答案

只需添加到Wen的答案中即可.或者,可以在使用df.Sex选择列时使用value_counts.

Just to add to Wen's answer. Alternatively, you can use value_counts while selecting the column with df.Sex.

df.Sex.value_counts()
    M    4
    F    1
Name: Sex, dtype: int64

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

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