While working on a dataset we sometimes need to get ranks of the columns based on the values in other features, rank can be defined in many ways like based on ascending order or decending order of the values in the feature.
This python source code does the following :
1. Creates and converts data dictionary into pandas dataframe
2. Creates new columns in the dataframe
3. Ranks dataframe in ascending and descending order
So this is the recipe on how we rank a Pandas DataFrame.
import pandas as pd
We have only imported pandas which is needed.
We have created a dictionary of data and passed it in pd.DataFrame to make a dataframe with columns 'first_name', 'last_name', 'age', 'Comedy_Score' and 'Rating_Score'.
raw_data = {'first_name': ['Sheldon', 'Raj', 'Leonard', 'Howard', 'Amy'],
'last_name': ['Copper', 'Koothrappali', 'Hofstadter', 'Wolowitz', 'Fowler'],
'age': [42, 38, 36, 41, 35],
'Comedy_Score': [9, 7, 8, 8, 5],
'Rating_Score': [25, 25, 49, 62, 70]}
df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age',
'Comedy_Score', 'Rating_Score'])
print(df)
We want to rank the dataframe on the basis of column 'age', for better understanding we will rank on ascending as well as decending order of age. But before using rank function let us first look into its parameters.
df['Hierarchy_Rank'] = df['age'].rank(ascending=True)
print(df)
df['Hierarchy_Rank'] = df['age'].rank(ascending=False)
print(df)
So the output comes as
first_name last_name age Comedy_Score Rating_Score 0 Sheldon Copper 42 9 25 1 Raj Koothrappali 38 7 25 2 Leonard Hofstadter 36 8 49 3 Howard Wolowitz 41 8 62 4 Amy Fowler 35 5 70 first_name last_name age Comedy_Score Rating_Score Hierarchy_Rank 0 Sheldon Copper 42 9 25 5.0 1 Raj Koothrappali 38 7 25 3.0 2 Leonard Hofstadter 36 8 49 2.0 3 Howard Wolowitz 41 8 62 4.0 4 Amy Fowler 35 5 70 1.0 first_name last_name age Comedy_Score Rating_Score Hierarchy_Rank 0 Sheldon Copper 42 9 25 1.0 1 Raj Koothrappali 38 7 25 3.0 2 Leonard Hofstadter 36 8 49 4.0 3 Howard Wolowitz 41 8 62 2.0 4 Amy Fowler 35 5 70 5.0