I have a data frame like this:
JavaScript
x
16
16
1
print(df)
2
3
0 1 2
4
0 354.7 April 4.0
5
1 55.4 August 8.0
6
2 176.5 December 12.0
7
3 95.5 February 2.0
8
4 85.6 January 1.0
9
5 152 July 7.0
10
6 238.7 June 6.0
11
7 104.8 March 3.0
12
8 283.5 May 5.0
13
9 278.8 November 11.0
14
10 249.6 October 10.0
15
11 212.7 September 9.0
16
As you can see, months are not in calendar order. So I created a second column to get the month number corresponding to each month (1-12). From there, how can I sort this data frame according to calendar months’ order?
Advertisement
Answer
Use sort_values
to sort the df by a specific column’s values:
JavaScript
1
18
18
1
In [18]:
2
df.sort_values('2')
3
4
Out[18]:
5
0 1 2
6
4 85.6 January 1.0
7
3 95.5 February 2.0
8
7 104.8 March 3.0
9
0 354.7 April 4.0
10
8 283.5 May 5.0
11
6 238.7 June 6.0
12
5 152.0 July 7.0
13
1 55.4 August 8.0
14
11 212.7 September 9.0
15
10 249.6 October 10.0
16
9 278.8 November 11.0
17
2 176.5 December 12.0
18
If you want to sort by two columns, pass a list of column labels to sort_values
with the column labels ordered according to sort priority. If you use df.sort_values(['2', '0'])
, the result would be sorted by column 2
then column 0
. Granted, this does not really make sense for this example because each value in df['2']
is unique.