Implementation of Sort Function sort_values of of Pandas

  • 2021-07-13 05:35:30
  • OfStack

1. Use of the sort_values () function

The sort_values () function in pandas is similar in principle to order by in SQL and sorts a dataset by the data in a field, either by the data in a specified column or by the data in a specified row.

2. Specific arguments for the sort_values () function

Usage:


DataFrame.sort_values(by= ' ##',axis=0,ascending=True, inplace=False, na_position= ' last')

Parameter description

参数 说明
by 指定列名(axis=0或'index')或索引值(axis=1或'columns')
axis 若axis=0或'index',则按照指定列中数据大小排序;若axis=1或'columns',则按照指定索引中数据大小排序,默认axis=0
ascending 是否按指定列的数组升序排列,默认为True,即升序排列
inplace 是否用排序后的数据集替换原来的数据,默认为False,即不替换
na_position {‘first',‘last'},设定缺失值的显示位置

3. Examples of usage of sort_values

Create a data box


# Use a dictionary dict Create a data box 
import numpy as np
import pandas as pd
df=pd.DataFrame({'col1':['A','A','B',np.nan,'D','C'],
         'col2':[2,1,9,8,7,7],
         'col3':[0,1,9,4,2,8]
})
print(df)

>>>
 col1 col2 col3
0  A   2   0
1  A   1   1
2  B   9   9
3 NaN   8   4
4  D   7   2
5  C   7   8

Sort by column 1 and put the null value of this column first


# Pursuant to paragraph 1 Column, and put the null value of the column first 
print(df.sort_values(by=['col1'],na_position='first'))
>>>
 col1 col2 col3
3 NaN   8   4
0  A   2   0
1  A   1   1
2  B   9   9
5  C   7   8
4  D   7   2

Sort in descending numeric order by column 2. 3


# Pursuant to paragraph 2. 3 Column, numeric descending sort 
print(df.sort_values(by=['col2','col3'],ascending=False))
>>>
 col1 col2 col3
2  B   9   9
3 NaN   8   4
5  C   7   8
4  D   7   2
0  A   2   0
1  A   1   1

Sort according to the values in Column 1, sort in descending order, and replace the original data


# According to Article 1 Column, sorted in descending order, and replaces the original data 
df.sort_values(by=['col1'],ascending=False,inplace=True,
           na_position='first')
print(df)
>>>
 col1 col2 col3
3 NaN   8   4
4  D   7   2
5  C   7   8
2  B   9   9
1  A   1   1
0  A   2   0

Sort in descending order by the value of the row with index value 0, that is, the first row


x = pd.DataFrame({'x1':[1,2,2,3],'x2':[4,3,2,1],'x3':[3,2,4,1]}) 
print(x)
# By index value is 0 The line of, that is, the 1 To sort in descending order by the value of the row 
print(x.sort_values(by =0,ascending=False,axis=1))
>>>
  x1 x2 x3
0  1  4  3
1  2  3  2
2  2  2  4
3  3  1  1
  x2 x3 x1
0  4  3  1
1  3  2  2
2  2  4  2
3  1  1  3

Related articles: