Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Dataframe sort by a column [duplicate]

I have a Python Pandas Data Frame. The df has 2 columns, I would like to sort the df by the second column.

   Kappa_prod   Angle
0   0.004511    -5.457840
1   0.003977    -5.312861
2   0.004476    -5.311292
3   0.003644    -117.579594
4   0.003306    -117.542817

I would like to sort the df by Angle (ascending order).

like image 946
Ayan Mitra Avatar asked Sep 06 '17 10:09

Ayan Mitra


People also ask

How do you find duplicates in a DataFrame column?

To find duplicate columns we need to iterate through all columns of a DataFrame and for each and every column it will search if any other column exists in DataFrame with the same contents already. If yes then that column name will be stored in the duplicate column set.

How do I sort pandas DataFrame based on column?

To sort the DataFrame based on the values in a single column, you'll use . sort_values() . By default, this will return a new DataFrame sorted in ascending order. It does not modify the original DataFrame.

How do I drop duplicate rows based on one column in pandas?

To remove duplicates of only one or a subset of columns, specify subset as the individual column or list of columns that should be unique. To do this conditional on a different column's value, you can sort_values(colname) and specify keep equals either first or last .

How do I filter duplicates in pandas?

We can use Pandas built-in method drop_duplicates() to drop duplicate rows. Note that we started out as 80 rows, now it's 77. By default, this method returns a new DataFrame with duplicate rows removed. We can set the argument inplace=True to remove duplicates from the original DataFrame.


1 Answers

You can use this:

df.sort_values("Angle", inplace=True)

By default ascending=True is passed to the above call. For more information check the documentation here.

like image 107
Mohamed Ali JAMAOUI Avatar answered Oct 14 '22 14:10

Mohamed Ali JAMAOUI