0

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?

1 Answer 1

2

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
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.