How to sort pandas dataframe from single column

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, the 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 the calendar month order?

+145
source share
5 answers

Use sort_values to sort df by specific column values:

 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 

If you want to sort by two columns, pass the list of column labels to sort_values with the column labels sorted according to the sort priority. If you use df.sort_values(['2', '0']) , the result will be sorted by column 2 then by column 0 . Of course, this does not make sense for this example, because each value in df['2'] unique.

+192
source

I tried the solutions described above and did not achieve results, so I found another solution that works for me. Ascending = False - the data order in descending order, by default it is True . I am using python 3.6.6 and pandas 0.23.4 version.

 final_df = df.sort_values(by=['2'], ascending=False) 

You can see more details in the panda documentation here .

+35
source

Just adding a few more data operations. Suppose we have a df dataframe, we can do several operations to get the desired results

 ID cost tax label 1 216590 1600 test 2 523213 1800 test 3 250 1500 experiment df['label'].value_counts().to_frame().reset_index()).sort_values('label', ascending=False) 

will give sorted label output as a dataframe

  index label 0 test 2 1 experiment 1 
+6
source

Just like another solution:

You can classify your string data (month name) and sort them as follows:

 df.rename(columns={1:'month'},inplace=True) df['month'] = pd.Categorical(df['month'],categories=['December','November','October','September','August','July','June','May','April','March','February','January'],ordered=True) df = df.sort_values('month',ascending=False) 

You will receive data sorted by month name, as you say when creating a Categorical object.

0
source

I have a question regarding .. Can I use df.sort_values โ€‹โ€‹(["column1", "column2"], ascending = False, True)? That is, if I want df to sort by both columns, the first in descending order, and the second in ascending order?

0
source

Source: https://habr.com/ru/post/1274419/


All Articles