AmirX
AmirX

Reputation: 2717

Pandas - sorting dataframe by two columns that one of them uses key

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.

Upvotes: 0

Views: 95

Answers (1)

el_oso
el_oso

Reputation: 1061

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')

Upvotes: 1

Related Questions