1

Lets say we have a df like below:

df = pd.DataFrame({'A':['y2','x3','z1','z1'],'B':['y2','x3','a2','z1']})

    A   B
0   y2  y2
1   x3  x3
2   z1  a2
3   z1  z1

if we wanted to sort the values on just the numbers in column A, we can do:

df.sort_values(by='A',key=lambda x: x.str[1])

    A   B
3   z1  z1
2   z1  a2
0   y2  y2
1   x3  x3

If we wanted to sort by both columns A and B, but have the key only apply to column A, is there a way to do that?

df.sort_values(by=['A','B'],key=lambda x: x.str[1])

Expected output:

    A   B
2   z1  a2
3   z1  z1
0   y2  y2
1   x3  x3
1
  • The easiest way would be to generate an extra column/separate Series with the output of your key function Commented Feb 19, 2021 at 14:06

1 Answer 1

3

You can sort by B, then sort by A with a stable method:

(df.sort_values('B')
   .sort_values('A', key=lambda x: x.str[1], kind='mergesort')
)

Output:

    A   B
2  z1  a2
3  z1  z1
0  y2  y2
1  x3  x3
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.