Python sort dataframe by one column

Code examples

26
0

sort a dataframe by a column valuepython

>>> df.sort_values(by=['col1'])
    col1 col2 col3
0   A    2    0
1   A    1    1
2   B    9    9
5   C    4    3
4   D    7    2
3   NaN  8    4
24
0

df sort values

>>> df.sort_values(by=['col1'], ascending = False)
    col1 col2 col3
0   A    2    0
1   A    1    1
2   B    9    9
5   C    4    3
4   D    7    2
3   NaN  8    4
10
0

sorting by column in pandas

#Python, Pandas
#Sorting dataframe df on the values of a column col1
#Temporary
df.sort_values(by=["col1"]) 

#Permanent
df.sort_values(by=["col1"], inplace = True)
4
0

df.sort_values(by='col1',asending=True)

>>> df.sort_values(by='col1', ascending=False)
  col1  col2  col3 col4
4    D     7     2    e
5    C     4     3    F
2    B     9     9    c
0    A     2     0    a
1    A     1     1    B
3  NaN     8     4    D
2
0

sort by two columns in pandas

df.sort_values(['a', 'b'], ascending=[True, False])
2
0

python sort dataframe by one column

#following is example of sorting by the column "2" in descending order
final_df = df.sort_values(by=['2'], ascending=False)

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................