current position:Home>You can easily get started with Excel. Python data analysis package pandas (VI): sorting

You can easily get started with Excel. Python data analysis package pandas (VI): sorting

2021-08-22 05:22:17 Excel catalyst

> I often listen to others Python How powerful in the data field , As a result, I studied for a long time , Even data processing is a death of trouble . Only later , It's not Python Data processing is powerful , But he has a data analysis artifact —— pandas

Preface

Sorting is a very basic data operation ,Excel The sorting function in is very flexible , You can sort rows and columns . Look at this article pandas How to do with Excel Same flexibility .

Excel Sort

Excel Sorting data in is very simple . as follows :

  • - Function card " data "," Sort " Button , The sort settings pop-up window appears
  • - We can set the sort by field
  • - First set up and press class Ascending , class If the same, press Chinese language and literature Grades are in ascending order
  • - At the top right of the pop-up window , Yes " Set up " Button , You can choose to sort rows or columns

pandas Sort

pandas Sorting in is also very simple , And basically with Excel The process principle is consistent , After all, it's all data tools . as follows :

  • - DataFrame.sort_values(), You can sort them
  • - The first parameter specifies the sort by key column
  • - The second parameter specifies the ascending and descending sequence adopted by the key column corresponding to the first parameter
  • - You can also set axis=1 , Sort by row ( There are few practical application scenarios )

Excel The sorting in can be customized , That is, let users customize the order ,pandas in , This function is not placed in sort_values To realize .

> actually Excel It also puts the custom sequence in a separate function

such as , hope Class according to "5,4,2,1,3,6,7" Sort , as follows :

  • - pandas You need to construct a column first Categorical , As an auxiliary column
  • - Categorical The second parameter at instantiation specifies the order
  • - Finally, call sort_values Method to complete custom sorting

> pandas Custom sorting in is relatively cumbersome , But we can completely encapsulate common operations by ourselves , You can also complete the custom sorting in one sentence

summary

  • - DataFrame.sort_values() , Sort the data
  • - The first parameter specifies the sort by key column
  • - The second parameter specifies the lifting sequence
  • - When a custom collation is required , It needs to be constructed Categorical Auxiliary column

This article is from WeChat official account. - Excel catalyst (ExcelCuiHuaJi)

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the [email protected] Delete .

Original publication time : 2019-08-22

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

copyright notice
author[Excel catalyst],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2021/08/20210822052204138J.html

Random recommended