numpy Provided in the library argsort() Function is used to sort , and pandas The library provides sort_values() Function is used to sort
DataFrame.sort_values(self, by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')[source]
There are six parameters ,by、axis、ascending、inplace、kind and na_position
by : str or list of str
Name or list of names to sort by.
if axis is 0 or ‘index’ then by may contain index levels and/or column labels
if axis is 1 or ‘columns’ then by may contain column levels and/or index labels
Changed in version 0.23.0: Allow specifying index or column level names.
If axis=0, that by Parameters are column labels , Sort vertically ;
If axis=1, that by The parameter is row label , Horizontal sort ;
axis : {0 or ‘index’, 1 or ‘columns’}, default 0
Axis to be sorted.
Select sort by row 、 Or sort by column
axis The default is 0,0 Expressed as vertical sort
axis by 1, Expressed as horizontal sort
ascending : bool or list of bool, default True
Sort ascending vs. descending. Specify list for multiple sort orders. If this is a list of bools, must match the length of the by.
The default is True Expressing ascending order , by False Representation of descending order , if by The parameter is a list , be ascending The parameter can be a list of the same length , Specify the ascending / descending sequence rules for each label
inplace : bool, default False
If True, perform operation in-place.
inplace The parameter defaults to False, if True, Then the sorted data is used to replace the original data
kind : {‘quicksort’, ‘mergesort’, ‘heapsort’}, default ‘quicksort’
Choice of sorting algorithm. See also ndarray.np.sort for more information. mergesort is the only stable algorithm. For DataFrames, this option is only applied when sorting on a single column or label.
Choose which sort algorithm , The default is quick sort
na_position : {‘first’, ‘last’}, default ‘last’
Puts NaNs at the beginning if first; last puts NaNs at the end.
Where to put the missing value , The default is last, That is, put the missing value at the end , Can be set to first That is, put the missing value first
import pandas as pd
data = pd.DataFrame([[1, 'Wang', 20], [2, 'Li', 20], [1, 'Wang', 21], [1, 'Wang', 20]], columns=['id', 'name', 'age'])
The data is
id name age
0 1 Wang 20
1 2 Li 20
2 1 Wang 21
3 1 Wang 20
Press id and age Sort ,id Ascending ,age Descending
data = data.sort_values(['id', 'age'], ascending=[True, False])
The result is
id name age
2 1 Wang 21
0 1 Wang 20
3 1 Wang 20
1 2 Li 20
Sort by row , Let's make the order from small to large appear on each line
data = data.sort_values(0, axis=1)
The result is
id age name
2 1 21 Wang
0 1 20 Wang
3 1 20 Wang
1 2 20 Li