This article is participating in Python Theme Month. See [activities]

The table used in this article is as follows:

Let’s look at the original case before sorting:

Import pandas as pd df = pd.read_excel(r 'c :\Users\admin\Desktop\ test.xlsx ') print(df)Copy the code

result:

Name Age Score 0 Xiao Ming 23.0 78 1 Xiao Gang NaN 89 2 Xiao Hong 876.0 65 3 Li Hua 65.0 89 4 Xiao Mei NaN 43 5 Zhang SAN 34.0 90 6 Li Si NaN 34 7 Wang Wu 98.5 87Copy the code

1. Sort by a list of values

Sorting by a column means that the entire table must be sorted by a column in ascending or descending order

Sorting requires the sort_values() method, in which the column name to be sorted is specified by the BY parameter, and the Ascending parameter indicates whether the order is ascending or descending.

1.1 Sort by a column of five missing values

1.1.1 Ascending order

The method defaults to ascending (that is, the default value for the Ascending parameter is True), and uses the BY parameter to specify the column name to be sorted

Df = pd.read_excel(r 'c :\Users\admin\Desktop\ test.xlsx ') print(df. Sort_values (by=[" score "]))Copy the code

result:

Name Age Score 6 Li Si NaN 34 4 Xiao Mei NaN 43 2 Xiao Hong 876.0 65 0 Xiao Ming 23.0 78 7 Wang Wu 98.5 87 1 Xiao Gang NaN 89 3 Li Hua 65.0 89 5 Zhang SAN 34.0 90Copy the code

1.1.2 Descending order

Simply set the ascending parameter to False to implement descending

Df = pd.read_excel(r 'c :\Users\admin\Desktop\ test.xlsx ') print(df.sort_values(by=[" score "], ascending=False))Copy the code

result:

Name Age Score 5 Zhang 3 34.0 90 1 Xiao Gang NaN 89 3 Li Hua 65.0 89 7 Wang Wu 98.5 87 0 Xiao Ming 23.0 78 2 Xiao Hong 876.0 65 4 Xiao Mei NaN 43 6 Li Si NaN 34Copy the code

1.2 Sort by the column with missing values

If there are missing values in the columns to be sorted, you can set the display position of the missing values by setting the na_position parameter

1.2.1 Missing values are shown at the end

By default, this method displays the missing value at the end (the default value for the na_position parameter is last).

Df = pd.read_excel(r 'c :\Users\admin\Desktop\ test.xlsx ') print(df. Sort_values (by=[" score "]))Copy the code
Df = pd.read_excel(r 'c :\Users\admin\Desktop\ test.xlsx ') print(df. Sort_values (by=[" age "]))Copy the code

result:

Name Age Score 0 Xiao Ming 23.0 78 5 Zhang SAN 34.0 90 3 Li Hua 65.0 89 7 Wang Wu 98.5 87 2 Xiao Hong 876.0 65 1 Xiao Gang NaN 89 4 Xiao Mei NaN 43 6 Li Si NaN 34Copy the code

1.2.2 Missing values are displayed first

By setting the value of the na_position parameter to first, missing values are displayed first

Df = pd.read_excel(r'C:\Users\admin\Desktop\ test.xlsx ') print(df. Sort_values (by=[" age "], na_position='first')Copy the code

result:

Name Age Score 1 Xiao Gang NaN 89 4 Xiao Mei NaN 43 6 Li Si NaN 34 0 Xiao Ming 23.0 78 5 Zhang SAN 34.0 90 3 Li Hua 65.0 89 7 Wang Wu 98.5 87 2 Xiao Hong 876.0 65Copy the code

2. Sort values in multiple columns

Sorting by multiple columns means sorting data in ascending and descending order at the same time. Repeat values in the first column are sorted by the second column, repeat values in the second column are sorted by the third column, and so on.

The names of the columns that need to be sorted in the sort_values() method are passed to the BY parameter as a list, and the ascending parameter for each column name that needs to be sorted is also passed to the ascending parameter as a list, with their lists corresponding to each other.

Df = pd.read_excel(r 'c :\Users\admin\Desktop\ test.xlsx ') print(df.sort_values(by=[" score ", "age "], ascending=[True, False])Copy the code

result:

Name Age Score 6 Li Si NaN 34 4 Xiao Mei NaN 43 2 Xiao Hong 876.0 65 0 Xiao Ming 23.0 78 7 Wang Wu 98.5 87 3 Li Hua 65.0 89 1 Xiao Gang NaN 89 5 Zhang SAN 34.0 90Copy the code

At this point, the grades are arranged in ascending order, and when the grades are the same, the grades are arranged in descending order by age.