简体   繁体   中英

Sorting is not working in Pandas dataframe

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?

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)

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM