Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions 135_nth_highest_salary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Get rid of duplicates, return an empty df if Nth highest salary does not exist, else sort the df using
# salary, get first N elements in descending order and give out the last element in that list.

import pandas as pd

def nth_highest_salary(employee: pd.DataFrame, N: int) -> pd.DataFrame:
df = employee[['salary']].drop_duplicates()
if N > len(df) or N <= 0:
return pd.DataFrame([None], columns=[f'getNthHighestSalary({N})'])
return (df.sort_values(by=['salary'], ascending=False).head(N).tail(1)
.rename(columns={'salary' : f'getNthHighestSalary({N})'}))
11 changes: 11 additions & 0 deletions 136_second_highest_salary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Get rid of duplicates, return an empty df if 2nd highest salary does not exist, else sort the df using
# salary, get first 2 elements in descending order and give out the last element in that list.

import pandas as pd

def second_highest_salary(employee: pd.DataFrame) -> pd.DataFrame:
df = employee[['salary']].drop_duplicates()
if len(df) < 2:
return pd.DataFrame([None], columns=['SecondHighestSalary'])
return (df.sort_values(by=['salary'], ascending=False).head(2).tail(1)
.rename(columns={'salary':'SecondHighestSalary'}))