rank( )

rank Function to return the subscript of sorting from small to large

1, By default,rank It is through“ Assign an average rank to each group” In a way that undermines the hierarchy
In [120]:obj = pd.Series([7,-5,7,4,2,0,4]) In [121]:obj.rank() Out [121]: 0 6.5
1 1.0 2 6.5 3 4.5 4 3.0 5 2.0 6 4.5 dtype: float64
2, Rank based on the order in which values appear in the original data
In [122]:obj.rank(method='first') Out [122]: 0 6.0 1 1.0 2 7.0 3 4.0 4 3.0 5
2.0 6 5.0 dtype: float64
3, Rank in descending order
In [123]:obj.rank(ascending=False, method='max') Out [123]: 0 2.0 1 7.0 2 2.0 3
4.0 4 5.0 5 6.0 6 4.0 dtype: float64
4, If yesDataFrame Sort, It can be based onaxis Specify the axis to sort
In [136]: frame=pd.DataFrame({'b':[5,7,-3,2],'a':[0,1,0,1],'c':[-2,5,8,-3]}) In
[137]: frame Out[137]: a b c 0 0 5 -2 1 1 7 5 2 0 -3 8 3 1 2 -3 In [138]: frame
.rank(axis=0) Out[138]: a b c 0 1.5 3.0 2.0 1 3.5 4.0 3.0 2 1.5 1.0 4.0 3 3.5
2.0 1.0 In [139]: frame.rank(axis=1) Out[139]: a b c 0 2.0 3.0 1.0 1 1.0 3.0 2.0
2 2.0 1.0 3.0 3 2.0 3.0 1.0
method Options for method

Tables Are
average default： In equal groups, Assign an average rank to each value
min Use the minimum ranking of the entire group
max Use the maximum ranking for the entire group
first Rank by the order in which values appear in the original data

30天阅读排行