asdasidw
asdasidw

Reputation: 3

Multiple column sorting not sorting by all provided columns

A           B                C            D           E
----------------------------------------------------------
85          0                0            0           0
399         0                0            0           0
35          0                0            0           0
206         0                0            0           0
24          0                0            0           9
341         0                0            1           0
6           0                0            4           0
19          0                0            4           0
21          6                0            6           0
3           7                1            4           2
170         0                2            0           0
150         0                2            0           0
216         0                2            1           0
336         0                2            4           0
2           0                2            4           0
6           0                3            0           0
53          0                3            0           0
40          0                3            0           0
23          0                3            1           0

Above is my final output. Below is my sorting code -

df= df.sort_values(by=['C', 'D', 'E', 'B', 'A'], ascending=[True, True, True, True, False])

My data get's correctly sorted by C,D,E,B however, the final A sorting is very incorrect. Why is it not properly getting sorted?

Upvotes: 0

Views: 39

Answers (1)

DeepSpace
DeepSpace

Reputation: 81604

The order 6, 53, 40 suggests that A column contains strings, not integers. As a result it is being sorted lexicographically instead of numerically.

Try

df['A'] = df['A'].astype(int)

and try the sorting again.

If relevant, make sure to convert all the other columns to integers as well, otherwise you will have the same problem when dealing with 'numbers' with 2 or more digits.

Upvotes: 4

Related Questions