Skip to content Skip to sidebar Skip to footer

Pandas Groupby: Percentage Above Threshold

I have a DataFrame that I'm looking to use a groupby on but I'm looking for a little bit of an unusual function to aggregate with. I would like to get the percentage of observation

Solution 1:

>>> df.groupby('day')['value'].apply(lambda c: (c>0).sum()/len(c))
day
1      0.333333
2      0.666667
3      0.333333
4      1.000000
Name: value, dtype: float64

Post a Comment for "Pandas Groupby: Percentage Above Threshold"