pandas ranking and ranking

sorting data according to conditions is an important built-in operation. To sort rows or columns (in dictionary order), use the sort? Index method, which returns a new sorted object.

frame= DataFrame(np.arange(8).reshape(2,4),
				index=['one','two'],columns=[list('dabc')])
frame
Out[5]: 
     d  a  b  c
one  0  1  2  3
two  4  5  6  7

frame.sort_index()
Out[9]: 
     d  a  b  c
one  4  5  6  7
two  0  1  2  3

frame.sort_index(axis=1)
Out[11]: 
     a  b  c  d
two  1  2  3  0
one  5  6  7  4

frame.sort_index(axis=1,ascending=False)#Reverse sorting
Out[12]: 
     d  c  b  a
two  0  3  2  1
one  4  7  6  5

Assign a value to by and sort by the value of one or more rows and columns

f2 = DataFrame({'b':[4,7,-3,2],'a':[0,1,0,1]})
f2
Out[55]: 
   b  a
0  4  0
1  7  1
2 -3  0
3  2  1


f2.sort_index(by='b')
__main__:1: FutureWarning: by argument to sort_index is deprecated,
 please use .sort_values(by=...)
Out[59]: 
   b  a
2 -3  0
3  2  1
0  4  0
1  7  1

#It is recommended to use. sort_values(by =
f2.sort_values(by='b')
Out[60]: 
   b  a
2 -3  0
3  2  1
0  4  0
1  7  1

Sort by priority

f2.sort_values(by=['a','b'])
Out[61]: 
   b  a
2 -3  0
0  4  0
3  2  1
1  7  1

f2.sort_values(by=['b','a'])
Out[62]: 
   b  a
2 -3  0
3  2  1
0  4  0
1  7  1

Sort rows

f2.sort_values(by=[1],axis=1)
Out[64]: 
   a  b
0  0  4
1  1  7
2  0 -3
3  1  2

f2.sort_values(by=[1,2],axis=1)
Out[65]: 
   a  b
0  0  4
1  1  7
2  0 -3
3  1  2

Rank rank()

obj
Out[66]: 
0    7
1   -5
2    7
3    4
4    2
5    0
6    4
dtype: int64
method Explain
'average' Default: assign average ranking to each value in the desired component
'min' Use the minimum ranking of the entire group
'max' Use the maximum ranking for the entire group
'first' Rank values in the order they appear in the original data
obj.rank()
Out[67]: 
0    6.5
1    1.0
2    6.5
3    4.5
4    3.0
5    2.0
6    4.5
dtype: float64
obj.rank(method='min')
Out[69]: 
0    6.0
1    1.0
2    6.0
3    4.0
4    3.0
5    2.0
6    4.0
dtype: float64
obj.rank(method='max')
Out[70]: 
0    7.0
1    1.0
2    7.0
3    5.0
4    3.0
5    2.0
6    5.0
dtype: float64
obj.rank(method='first',ascending=False)
Out[71]: 
0    1.0
1    7.0
2    2.0
3    3.0
4    5.0
5    6.0
6    4.0
dtype: float64

Similarly, DataFrame can also rank in ranks

Posted by phpconnect on Wed, 25 Dec 2019 14:02:51 -0800