Rank Sort Series DataFrames in Python Pandas Numpy
In this quick notes, we will have a look at Rank and Sort in Series and DataFrames in Python
In [1]:
1 2 3 |
import numpy as np import pandas as pd from pandas import Series, DataFrame |
1 2 3 |
#make a series ser1 = Series(range(3), index=['C','A','B']) ser1 |
1 2 |
#sort index of series ser1.sort_index() |
1 2 |
#order or sort by values ser1.order() |
1 |
ser1.sort_values() |
1 |
from numpy.random import randn |
1 2 3 |
#make a series by passing in 10 random numbers ser2 = Series(randn(10)) ser2 |
1 2 |
#sort the values ser2.sort_values() |
1 2 |
#rank the values #### meaning the values are actually sorted based on rank ser2.rank() |
1 2 3 |
#make a third series and generate 10 random numbers ser3 = Series(randn(10)) ser3 |
1 2 |
#lets check the rank ser3.rank() |
1 2 3 |
#let's sort and then rank ser3.sort_values() ser3.rank() |