Pandas dataframe. Group by value and count

孤者浪人 提交于 2019-12-24 09:58:22

问题


I have the following table:

Days, Age,  Sex

5,    39,   F
4,    54,   M
4,    26,   M
5,    42,   M
4,    29,   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.


回答1:


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


来源:https://stackoverflow.com/questions/48253759/pandas-dataframe-group-by-value-and-count

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!