The rank() function is used to compute numerical data ranks (1 through n) along axis. By default, equal values are assigned a rank that is the average of the ranks of those values.
So this recipe is a short example on How to find rank in a pandas dataframe. Let's get started.
import pandas as pd
Let's pause and look at these imports. Pandas is generally used for performing mathematical operation and preferably over arrays.
df = pd.DataFrame({"A":[0, 1, 2, 3, 5, 9],
"B":[11, 5, 8, 6, 7, 8],
"C":[2, 5, 10, 11, 9, 8]})
Here we have setup a random dataset with some random values in it.
df['default_rank'] = df['B'].rank()
print(df)
Here we are applied rank to find out the rank order of column B.
Once we run the above code snippet, we will see:
Scroll down to the ipython file to look at the results.
We can see the how the rank are assigned. Equal values takes equal ranks, midway between other ranks.