I'm looking to perform some slicing on a dataframe and I am repeating the same slices on both the .mean()
and .std()
results. I am wondering if it would be possible to write a function were mean
or std
can be defined as an argument. If so, I may later expand these type of df
operations to others such as .min()
and .max()
. Admittedly a limited set, but I'm still curious.
So currently my code looks like this:
df_mean = df.groupby(level=1).mean()
df_std = df.groupby(level=1).std()
df_std_slice = df_std[(df_mean['A']>10)&(df_mean['B']==1).copy()
df_mean_slice = df_mean[(df_mean['A']>10)&(df_mean['B']==1).copy()
I was wondering if it could look similar to:
def df_slice(df,oper_arg):
df_mean = df_mean = df.groupby(level=1).mean() #I do need to have a df with mean data
df_{oper_arg} = df.groupby(level=1).{oper_arg}().copy()
df_slice = df_{oper_arg}[(df_mean['A']>10)&(df_mean['B']==1).copy()
return df_slice
CodePudding user response:
Sure you can! agg()
exists for this purpose:
def df_slice(df,oper_arg):
df_mean = df.groupby(level=1).mean()
df = df.groupby(level=1).agg(oper_arg).copy()
# notice ^^^^^^^^^^^^^^
df_slice = df[(df_mean['A']>10)&(df_mean['B']==1).copy()
return df_slice