Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - Apply aggregation on filtered dataframe

Tags:

python

pandas

GroupBy a column and aggregate one of the columns on filtered values.

Like in the given example below, I want to count the number of animals which are of gender 'male' for each of the 'kind' of animals

import pandas as pd
df = pd.DataFrame({'kind': ['cat', 'dog', 'cat', 'dog'],
                'height': [9.1, 6.0, 9.5, 34.0],
                'gender': ['male', 'female', 'female', 'female']})
df.groupby('kind').agg({'height': 'min', 'gender': lambda g: (g == 'male').count()})

Output that I get (which is wrong)

kind      height     gender
cat       9.1        2
dog       6.0        2

Expected output:

kind      height     gender
cat       9.1        1
dog       6.0        0
like image 405
peakit Avatar asked Nov 24 '25 02:11

peakit


1 Answers

Instead of count(), you can use sum().

df.groupby('kind').agg({'height': 'min', 'gender': lambda g: (g == 'male').sum()})
like image 119
maltodextrin Avatar answered Nov 25 '25 15:11

maltodextrin