How to make a group by in Pandas?
I have a DataFrame of this kind:
Id| Sex | ....
------------------
1 |male |....
2 |female|....
3 |female|....
4 |male |....
5 |male |....
As a result, I want to get something like
Sex | count
--------------
male | 3
female| 2
Tried :
df.groupby(['sex']).agg(['mean', 'count'])
But it seems that this is not the case
1
1 answers
Source DataFrame:
In [10]: df
Out[10]:
Id Sex
0 1 male
1 2 female
2 3 female
3 4 male
4 5 male
Number of occurrences for each value:
In [13]: df['Sex'].value_counts()
Out[13]:
male 3
female 2
Name: Sex, dtype: int64
The same thing in the form of DF using df.groupby().size()
:
In [11]: df.groupby(['Sex']).size().reset_index(name='count')
Out[11]:
Sex count
0 female 2
1 male 3
Grouping and calling multiple aggregating functions:
In [12]: df.groupby(['Sex'])['Id'].agg(['mean', 'count'])
Out[12]:
mean count
Sex
female 2.500000 2
male 3.333333 3
Your option is to return a DataFrame with "two-level" column names:
In [14]: df.groupby(['Sex']).agg(['mean', 'count'])
Out[14]:
Id
mean count
Sex
female 2.500000 2
male 3.333333 3
1
Author: MaxU, 2018-05-07 12:15:00