Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

looping over pandas aggregation in groupby

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)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading