0

Edited for clarity:

I have a dataframe in the following format

i    col1         col2  col3
0    00:00:00,1   10    1.7
1    00:00:00,2   10    1.5
2    00:00:00,3   50    4.6
3    00:00:00,4   30    3.4
4    00:00:00,5   20    5.6
5    00:00:00,6   50    1.8
6    00:00:00,9   20    1.9

...

That I'm trying to sort like this

 i    col1         col2  col3
0    00:00:00,1   10    1.7
1    00:00:00,2   10    1.5
4    00:00:00,5   20    5.6
3    00:00:00,9   20    1.9
4    00:00:00,4   30    3.4
5    00:00:00,3   50    4.6
6    00:00:00,6   50    1.8

...

I've tried df = df.sort_values(by = ['col1', 'col2'] which only works on col1. I understand that it may have something to do with the values being 'strings', but I can't seem to find a workaround for it.

3
  • do you want to sort col2 independently of the rest or is your example incorrect? Commented May 16, 2022 at 12:05
  • @mozway My example was incorrect. I updated to hopefully be more clear. Basically I'm trying to sort values group wise. Commented May 16, 2022 at 12:47
  • so df.sort_values(by=['col2', 'col1'])? can you provide the output of df.to_dict('list')? Commented May 16, 2022 at 13:29

2 Answers 2

1
df.sort_values(by = ['col2', 'col1']

Gave the desired result

Sign up to request clarification or add additional context in comments.

Comments

0

If need sort each column independently use Series.sort_values in DataFrame.apply:

c = ['col1','col2']
df[c] = df[c].apply(lambda x: x.sort_values().to_numpy())
#alternative
df[c] = df[c].apply(lambda x: x.sort_values().tolist())
print (df)
   i        col1  col2
0  0  00:00:00,1    10
1  1  00:00:01,5    20
2  2  00:00:10,0    30
3  3  00:01:00,1    40
4  5  01:00:00,0    50

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.