0

This is my dataframe:

df = pd.read_csv('https://raw.githubusercontent.com/AmirForooghi/stocks_example/main/xxx.csv')

I want to sort df by two columns: rsi and sector. For rsi I want ascending=False and for sector I want to use the key argument. The key is:

order_dic = {j: i for i, j in enumerate(['bank', 'naft', 'pey'])}

What I have tried so far is:

df = df.loc[df.sector.isin(['bank', 'naft', 'pey'])]  # just to clean the data a little bit
sorted_df = df.sort_values(by='rsi', ascending=False).sort_values(by='sector', key=lambda x:x.map(order_dic))

But it doesn't work. it is sorted by the key correctly but it is not sorted by rsi.

3
  • what's your expected output? Can you be more specific on exactly what doesn't work Commented Apr 16, 2021 at 7:45
  • @el_oso I want to sort it by two columns. If you run my code you can see that it is sorted by the key correctly but it is not sorted by rsi anymore Commented Apr 16, 2021 at 7:46
  • can you edit the question to include the output you get and the output you want Commented Apr 16, 2021 at 7:47

1 Answer 1

1

You need to use a stable sorting algorithm to preserve the order of the first sort. You can do this by setting kind='mergesort'

sorted_df = df.sort_values(by='rsi', ascending=False).sort_values(by='sector', key=lambda x:x.map(order_dic), kind='mergesort')
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.