Avixon
Avixon

Reputation: 123

dataframe.sort_values() buy multiple columns with key for one column

Lets say i have a dataframe that looks like this:

col1, col2, col3, col4
txt,c,fix,txt
txt,a,error,txt
txt,a,new,txt
txt,c,new,txt
txt,c,error,txt
txt,b,new,txt
txt,b,fix,txt

and desired output is:

col1, col2, col3, col4
txt,a,new,txt
txt,a,error,txt
txt,b,new,txt
txt,b,error,txt
txt,c,new,txt
txt,c,fix,txt
txt,c,error,txt

so col2 in ascending order, and col3 follows new->fix->error

So far I've sorted col3 with desired output with:

custom_dict = {'new': 0, 'fix': 1, 'error': 2}
csv_dataframe = csv_dataframe.sort_values(by=['col3'], key=lambda x: x.map(custom_dict))

But from my understanding using multiple columns with key does not work:

custom_dict = {'new': 0, 'fix': 1, 'error': 2}
csv_dataframe = csv_dataframe.sort_values(by=['col2', 'col3'], key=lambda x: x.map(custom_dict))

Is there a way around this, or some other implementation of key i can use to achieve desired output?

Upvotes: 0

Views: 339

Answers (1)

jezrael
jezrael

Reputation: 862601

Use replace for replace both columns:

custom_dict = {'new': 0, 'fix': 1, 'error': 2}
df = df.sort_values(by=['col2', 'col3'], key=lambda x: x.replace(custom_dict))

print (df)
  col1 col2   col3 col4
2  txt    a    new  txt
1  txt    a  error  txt
5  txt    b    new  txt
6  txt    b    fix  txt
3  txt    c    new  txt
0  txt    c    fix  txt
4  txt    c  error  txt

Upvotes: 2

Related Questions