I'm looking for shortering the code where I don't need to repeat multiple lambda functions. This code is working, want to optimize further. Any help would be appreciated.
for col in col_list:
f = { col: [lambda x: x.quantile(0.01), lambda x: x.quantile(0.05), lambda x: x.quantile(0.10), lambda x: x.quantile(0.15),
lambda x: x.quantile(0.20), lambda x: x.quantile(0.25), lambda x: x.quantile(0.30), lambda x: x.quantile(0.35)
]}
grpby_df = df.groupby('grpbycol').agg(f)
CodePudding user response:
GroupBy.quantile exists and it accepts a list of quantile values, so we can do
(df.groupby("grpbycol")[col]
.quantile([0.01, *np.arange(0.05, 0.40, 0.05)])
.unstack())
where np.arange helps generate that sequence, and unstack
at the end will move the quantile values to the columns part from a level of a multiindex.