In this quick notes, we will have a look at Rank and Sort in Series and DataFrames in Python

In [1]:

import numpy as np
import pandas as pd
from pandas import Series, DataFrame

 

In [3]:
#make a series
ser1 = Series(range(3), index=['C','A','B'])
ser1

 

Out[3]:
C    0
A    1
B    2
dtype: int32
In [4]:
#sort index of series
ser1.sort_index()

 

Out[4]:
A    1
B    2
C    0
dtype: int32
In [5]:
#order or sort by values
ser1.order()

 

C:\Users\adaba\Anaconda3\lib\site-packages\ipykernel\__main__.py:2: FutureWarning: order is deprecated, use sort_values(...)
  from ipykernel import kernelapp as app
Out[5]:
C    0
A    1
B    2
dtype: int32
In [6]:
ser1.sort_values()

 

Out[6]:
C    0
A    1
B    2
dtype: int32
In [7]:
from numpy.random import randn

 

In [8]:
#make a series by passing in 10 random numbers
ser2 = Series(randn(10))
ser2

 

Out[8]:
0   -0.201751
1    1.534109
2    0.542825
3    1.421174
4   -0.173771
5    0.002573
6   -0.490208
7   -0.765340
8   -2.546040
9   -0.456699
dtype: float64
In [10]:
#sort the values
ser2.sort_values()

 

Out[10]:
8   -2.546040
7   -0.765340
6   -0.490208
9   -0.456699
0   -0.201751
4   -0.173771
5    0.002573
2    0.542825
3    1.421174
1    1.534109
dtype: float64
In [12]:
#rank the values #### meaning the values are actually sorted based on rank
ser2.rank()

 

Out[12]:
8     1.0
7     2.0
6     3.0
9     4.0
0     5.0
4     6.0
5     7.0
2     8.0
3     9.0
1    10.0
dtype: float64
In [16]:
#make a third series and generate 10 random numbers
ser3 = Series(randn(10))
ser3

 

Out[16]:
0    1.103402
1    1.399324
2   -0.333999
3    0.024969
4   -0.146458
5    0.699460
6   -0.916496
7   -0.553974
8   -0.932767
9   -0.947196
dtype: float64
In [18]:
#lets check the rank
ser3.rank()

 

Out[18]:
0     9.0
1    10.0
2     5.0
3     7.0
4     6.0
5     8.0
6     3.0
7     4.0
8     2.0
9     1.0
dtype: float64
In [20]:
#let's sort and then rank
ser3.sort_values()
ser3.rank()

 

Out[20]:
9     1.0
8     2.0
6     3.0
7     4.0
2     5.0
4     6.0
3     7.0
5     8.0
0     9.0
1    10.0
dtype: float64
This is part of lectures on Learning Python for Data Analysis and Visualization by Jose Portilla on Udemy.

 

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *