程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Pandas sort_ values

編輯:Python

pandas Sort sort_values

  • by Parameters
  • axis Parameters
  • ascending Parameters
  • inplace Parameters
  • kind Parameters
  • na_position Parameters
  • Example

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 Parameters

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 Parameters

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 Parameters

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 Parameters

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 Parameters

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 Parameters

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

Example

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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved