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
Instead of count(), you can use sum().
df.groupby('kind').agg({'height': 'min', 'gender': lambda g: (g == 'male').sum()})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With