author :Peter edit :Peter
Hello everyone , I am a Peter~
In a previous article , Describes in detail how to use pandas Built in functions for sort_values To sort the data . This article explains how to use custom methods to achieve sorting :
<!--MORE-->
First simulate a simple data :
import pandas as pd import numpy as np df = pd.DataFrame({ "nick":["aaa","bbb","aba","abc","cac","ccc"], # nickname "math":[100,120,130,111,100,128], # mathematics "english":[140,80,120,90,125,116], # English "size":["S","M","L","XS","XL","L"] # Clothing size }) df
DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last', # last,first; The default is last ignore_index=False, key=None)
The specific interpretation of the parameter is :
Here are a few simple examples to review sort_values Use :
adopt nick Field sorting , Strings are based on letters ASCII code ; The default is ascending from small to large . Same first letter , Compare the second , Reason by analogy :
Arrange in ascending order according to the size of the values :
You can change the sorting method to descending :
Sorting multiple fields at the same time , Default is also ascending . When the value of the first field is the same , Then arrange them in ascending order according to the second field
Assign different sorting methods to different fields :
Then compare the two different ways completely :
Above is sort_values Methods .
Use sort_values Methods are used to sort by the size of the built-in alphabetic or numeric data , When you encounter the following situations , How to operate ?
When we according to the size of the clothes size Sort by , And what you get is :
Obviously, this sort of sorting is not what we expected , In our cognition :
How to solve this problem ? There are two ways :
1、 Find each size The value size corresponding to the order of
2、 Generate new fields order
3、 We are right. order Sort
CategoricalDtype Is a type of categorical data with a category and order , Can create our custom sort data type . Official website address :
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.CategoricalDtype.html
1、 Specify a classified data type CategoricalDtype
category_size = pd.CategoricalDtype( ['XS', 'S', 'M', 'L', 'XL'], ordered=True) category_size
2、 take size The field is set to the above CategoricalDtype type
3、 We're right size Use sort_values We can achieve our goal , And the above map The effect of mapping is the same
And by looking at df Data type of , We also see size The type is category: