问题
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