I have a pandas DataFrame with Data
and Group
s and I want to perform multiple functions using the agg
-method.
from scipy.stats import iqr, norm
import pandas as pd
df = pd.DataFrame({'Data':[1,2,3,5,10,5,3,3,4,1], 'Group':[1,2,2,1,1,1,2,2,2,1]})
df.groupby('Group')['Data'].agg(['median', iqr])
which works fine. But now I want to slice the groups befor I perform the operations.
The problem is, that
df.groupby('Group')['Data'].iloc[2:-2].agg(['median', iqr])
thows an error because slicing is not supported for PandasGroupByObjects. An error comes for
df.groupby('Group')['Data'].nth[2:-2].agg(['median', iqr])
too, because the combinations is not allowed.
This post shows that a lambda function
can be used to slice the values.
I tried to use this, but
df.groupby('Group')['Data'].agg(lambda x: [np.median(x.iloc[2:-2]), iqr(x.iloc[2:-2])])
returns a DataFrames with lists in it, which is not what I want.
How to apply the slicing and get a well formated DataFrame as return value?
Wanted output:
median iqr
Group
1 10.0 0.0
2 3.0 0.0