0

I want to sort the whole dataframe based on a column. I did the following:

import pandas as pd

something = [[1, "p", 2], [3, "t", 5], [6, "u", 10], [1, "p", 2], [4, "l", 9], [1, "t", 2], [3, "t", 5], [6, "c", 10], [1, "p", 2], [4, "l", 9]]
test = pd.DataFrame(something)
print(test) #before sorting
test.columns = ['id', 'state', 'level']
test.sort_values(by=['id'], ascending=True)
print(test) #after sorting

I get the following output:

0  1   2
0  1  p   2
1  3  t   5
2  6  u  10
3  1  p   2
4  4  l   9
5  1  t   2
6  3  t   5
7  6  c  10
8  1  p   2
9  4  l   9

#after sorting
   id state  level
0   1     p      2
1   3     t      5
2   6     u     10
3   1     p      2
4   4     l      9
5   1     t      2
6   3     t      5
7   6     c     10
8   1     p      2
9   4     l      9

I checked the data in the first column and it is of int data type. But the sorting does not work. What am I doing wrong?

1
  • 1
    you dont assign the results back test = test.sort_values(by=['id'], ascending=True) thats the reason Commented Jun 4, 2021 at 18:21

3 Answers 3

1

You have to pass inplace=True argument like this:

test.sort_values(by=['id'], ascending=True, inplace=True)

If you don't pass that argument, it will return a dataframe object, so you can use it like this too:

test = test.sort_values(by=['id'], ascending=True)
Sign up to request clarification or add additional context in comments.

Comments

0

It does work, but test.sort_values(by=['id'], ascending=True) returns a new dataframe. In order to alter the original one, you need to add inplace=True:

test.sort_values(by=['id'], ascending=True, inplace=True)

Comments

0

it is working:

import pandas as pd

something = [[1, "p", 2], [3, "t", 5], [6, "u", 10], [1, "p", 2], [4, "l", 9], [1, "t", 2], [3, "t", 5], [6, "c", 10], [1, "p", 2], [4, "l", 9]]
test = pd.DataFrame(something)
test.columns = ['id', 'state', 'level']
print(test)
sorted_test = test.sort_values(by=['id'], ascending=True)
print(sorted_test)

or:

import pandas as pd

something = [[1, "p", 2], [3, "t", 5], [6, "u", 10], [1, "p", 2], [4, "l", 9], [1, "t", 2], [3, "t", 5], [6, "c", 10], [1, "p", 2], [4, "l", 9]]
test = pd.DataFrame(something)
test.columns = ['id', 'state', 'level']
print(test)
test.sort_values(by=['id'], ascending=True, inplace=True)
print(test)

or:

import pandas as pd

something = [[1, "p", 2], [3, "t", 5], [6, "u", 10], [1, "p", 2], [4, "l", 9], [1, "t", 2], [3, "t", 5], [6, "c", 10], [1, "p", 2], [4, "l", 9]]
test = pd.DataFrame(something)
test.columns = ['id', 'state', 'level']
print(test)
test = test.sort_values(by=['id'], ascending=True)
print(test)

returns:

   id state  level
0   1     p      2
1   3     t      5
2   6     u     10
3   1     p      2
4   4     l      9
5   1     t      2
6   3     t      5
7   6     c     10
8   1     p      2
9   4     l      9

   id state  level
0   1     p      2
3   1     p      2
5   1     t      2
8   1     p      2
1   3     t      5
6   3     t      5
4   4     l      9
9   4     l      9
2   6     u     10
7   6     c     10

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.