0

enter image description here

My code of decile portfolio by RETl1 and VOL

merged['decile_RETl1'] = merged.groupby(['Date'])['RET_l1'].transform(lambda x: pd.qcut(x, 10, duplicates='drop',labels=False))

merged['decile_RETl1']=merged['decile_RETl1']+1

merged['decile_turnover'] = merged.groupby(['Date'])['VOL'].transform(lambda x: pd.qcut(x, 10, duplicates='drop',labels=False))

merged['decile_turnover']=merged['decile_turnover']+1

I'm able to create a decile portfolio by RETl1 and VOL each but I don't know how to create conditional decile sorts portfolio by first on RETl1 and then on VOL within RETl1 deciles.

Please answer me if you know how to create conditional decile sorts portfolio. Thank you very much.

Corralien
  • 109,409
  • 8
  • 28
  • 52
M olly
  • 1
  • Welcome to Stackoverflow. For better answers, it would be helpful if you present an example for the desired output for a small sample of data. The original data is not critical, share a small sample to demonstrate the dataframe structure and the output that you need. Good luck! – Yanirmr Dec 01 '21 at 19:46
  • Please provide enough code so others can better understand or reproduce the problem. – Community Dec 02 '21 at 00:59

1 Answers1

0

If you're interested in sorting a Pandas DataFrame by multiple columns, as I understand from your question, you can use sort_values method.

For example:

merged = merged.sort_values(["RETl1", "VOL"], ascending = (True, True))

More relevant insight in that question.

Yanirmr
  • 923
  • 8
  • 25