Menu Close

How to Find the Nth Highest Salary Using Pandas

How to Find the Nth Highest Salary Using Pandas

In This Pandas article, we are about to find the Nth highest salary using Pandas with the help of the examples. Here, we will explore two scenarios: Nth highest salary in the whole dataset and Nth highest salary in a specific group like department, country, etc. Here, Nth means, any positive integer like 2nd highest salary, 3rd highest salary, 4th highest salary, etc.

A few days ago, I wrote an article on how to find the Nth highest salary using PySpark but in today’s article, I am going to achieve the same thing with the help of the Pandas library.

I have already prepared small CSV datasets along with some records. Throughout this article, we will find the 3rd and 2nd highest salaried employees in complete data and each department.

How to Find the Nth Highest Salary Using Pandas

Let’s Begin!

Find the Nth Highest Salary Using Pandas

Pandas provides some built-in DataFrame methods that will be helpful to finding the Nth highest salary. Now, let’s see the process of getting 3rd and 2nd highest getting a salaried employee.

Without Considering Department

Using this example, You can find the Nth highest salaried employee in the whole dataset.

Example: Find 3rd Highest Salary in Whole Data

import pandas as pd
df = pd.read_csv('../../pyspark_tutorials/sample_data.csv')
# Getting nth highest salaried employee in whole dataset
n = 3
nth_highest_salary = df.nlargest(3, columns='salary', keep="first").reset_index().loc[[2]]
print(nth_highest_salary)

Output

Find the 3rd Highest Salary Using Pandas

This is the way to find the 3rd highest-getting salaried employee from the complete dataset.

With Considering Department

In this section, you will get a solution to find the Nth highest salaried employee in a specific group like a department.

Example: Find 2nd Highest Salary in Each Department

import pandas as pd
df = pd.read_csv('../../pyspark_tutorials/sample_data.csv')
# Getting nth highest salaried employee in specific department
n = 2
df.sort_values(by=['salary'], ascending=False, inplace=True)
nth_highest_salary = df.groupby("department").nth(1)
print(nth_highest_salary)

Code Explanation:

  • Importing the pandas library as pd
  • Loading data frame CSV file.
  • Defined a variable n which indicates several highest salaried employees we want to get.
  • Sort the dataset set by salary in descending order.
  • Using the df.groupby("department") to apply the group by the department. The groupby() method will create three groups for three departments and for each group there will be three rows as you can see in the above dataset, each department has three rows and rows will be sorted in descending order by salary in each group.
  • Using .nth(1) to get the second row from the group because the index always starts from 0 that’s why I am passing 1 into the nth() method because 1 indicates the second row.

Output

Find the 2nd Highest Salary Using Pandas

This is how you can find the Nth highest salary using Pandas in a specific department.


Pandas Useful Articles:


Conclusion

So in this tutorial guide, we have seen how to find the Nth highest salary using Pandas by considering the department and without considering the department.

This question might be asked by interviewers Python Developer, Data Engineer, Data Analyst, and Data Scientist

If you found this article helpful, please don’t forget to bookmark this page in your browser.

Happy Coding!

How to Get Top 10 Lowest Values in Pandas DataFrame
A Comprehensive Guide to Pandas Data Structures

Related Posts