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?
test = test.sort_values(by=['id'], ascending=True)thats the reason