1

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)
1
  • Can you add some mock data and expected output to this question. Commented Jan 17, 2023 at 18:42

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.