Pandas data analysis interview basic questions (8)

Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-In Event, click to view the event details .

Sorting is a very basic requirement in data analysis. This article introduces the sorting of Dataframe data.

Question: How to sort DataFrame data?

(Difficulty: medium)

Analytical DataFrames can be sorted in two ways:

  • Sort by tags
  • Sort by actual value

Sort by label
Using the sort_index()method, axisspecify the row label or column label through the parameter, and ascendingspecify the ascending or descending order by the parameter. By default, row labels are sorted in ascending order. The specific usage is as follows:

# 按行标签排序
sort_df = df.sort_index()
sort_df
# 按行标签降序排序
sort_df = df.sort_index(ascending=False)
sort_df
# 按列标签排列
sort_df = df.sort_index(axis=1)
sort_df
# 按列标签降序排列
sort_df = df.sort_index(axis=1, ascending=False)
sort_df
复制代码

The sort by actual value
sort_values() method can sort by value, and it accepts a byparameter to specify the column name to sort by. The format is as follows:

DataFrame.sort_values(by, axis=0, ascending=True, inplace=False)
复制代码
  • axis: When the default is 0, it means vertical sorting; when axis=1, it means horizontal sorting.

  • by: String or list, indicating the row or column name to be sorted.

  • ascending: The default is True ascending order, which can be in the form of [True, False], indicating that the first field is ascending and the second is descending, but it should correspond to the content of by.

  • inplace: Whether to overwrite the original value.

# 按score的值进行升序排序
sort_df = df.sort_values(by='score')
sort_df
# 先按class的值升序排序,一样再按score的值降序排序
sort_df = df.sort_values(by=['class','score'], ascending=[True,False])
sort_df
复制代码

The resulting output is:

      id | name  | score | class |
| - | -- | ----- | ----- | ----- |
| 0 | 1  | one   | 90    | 1     |
| 1 | 2  | two   | 88    | 1     |
| 2 | 3  | three | 89    | 2     |
| 3 | 4  | four  | 65    | 2     |
| 4 | 5  | five  | 95    | 3     |
复制代码

Extension : After sorting the Dataframe data vertically, if the row index is out of order, we can use the reset_index()method to reset the number.

sort_df = sort_df.reset_index(drop=True)
sort_df
复制代码

It's not easy to be original. If you guys find it helpful, please give a like and go~

Finally, I would like to thank my girlfriend for her tolerance, understanding and support in work and life!

Guess you like

Origin juejin.im/post/7077841786525188110