Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Machine Learning by (47.6k points)

I have a data frame like this:

print(df)

        0          1 2

0   354.7      April 4.0

1    55.4     August 8.0

2   176.5   December  12.0

3    95.5   February   2.0

4    85.6   January   1.0

5     152     July 7.0

6   238.7       June 6.0

7   104.8      March 3.0

8   283.5        May 5.0

9   278.8   November  11.0

10  249.6    October 10.0

11  212.7  September   9.0

As you can see, months are not in the 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?

1 Answer

0 votes
by (33.1k points)

You can simply use python’s sort_values() function to sort dataframe by specific column name or value.

In [18]:

df.sort_values('2')

Out[18]:

        0          1        2

4    85.6   January         1.0

3    95.5   February        2.0

7   104.8      March        3.0

0   354.7      April        4.0

8   283.5        May        5.0

6   238.7       June        6.0

5   152.0       July        7.0

1    55.4     August        8.0

11  212.7  September        9.0

10  249.6    October        10.0

9   278.8   November        11.0

2   176.5   December        12.0

Hope this answer helps.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...