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.
Let’s Begin!
Headings of Contents
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
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. Thegroupby()
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
This is how you can find the Nth highest salary using Pandas in a specific department.
Pandas Useful Articles:
- How to Add Date Column in Pandas DataFrame
- How to Get Day Name from Date in Pandas DataFrame
- How to Split String in Pandas DataFrame Column
- How to Drop Duplicate Rows in Pandas DataFrame
- How to Get Top 10 Lowest Values in Pandas DataFrame
- How to Get Top 10 Highest Values in Pandas DataFrame
- How to Display First 10 Rows in Pandas DataFrame
- How to Explode Multiple Columns in Pandas
- How to use GroupBy in Pandas DataFrame
- How To Add a Column in Pandas Dataframe
- How to Replace Column Values in Pandas DataFrame
- How to Convert Excel to JSON in Python
- How to convert DataFrame to HTML in Python
- How to Delete a Column in Pandas DataFrame
- How to convert SQL Query Result to Pandas DataFrame
- How to Convert Dictionary to Excel in Python
- How to Convert Excel to Dictionary in Python
- How to Delete Column from Pandas DataFrame
- How to Rename Column Name in Pandas DataFrame
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!