Reputation: 61
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?
Upvotes: 0
Views: 716
Reputation: 2436
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
Upvotes: 0
Reputation: 5802
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)
Upvotes: 0
Reputation: 1438
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)
Upvotes: 1