pandas 熊猫数据框。按值和计数分组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48253759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas dataframe. Group by value and count
提问by Jerry
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:
我想分别用 F 和 M 计算行数。以下命令有效,但我对表示不满意:
df.groupby("Sex").count()
What will be the best way to do it? Thank you.
最好的方法是什么?谢谢你。
回答by Tai
Just to add to Wen's answer. Alternatively, you can use value_counts
while selecting the column with df.Sex
.
只是为了补充温的回答。或者,您可以在使用value_counts
选择列时使用df.Sex
。
df.Sex.value_counts()
M 4
F 1
Name: Sex, dtype: int64