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
13 changes: 13 additions & 0 deletions 01-NthHighestSalary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Problem 1 - Nth Highest Salary (https://leetcode.com/problems/nth-highest-salary/solution/)
import pandas as pd

def nth_highest_salary(employee: pd.DataFrame, N: int) -> pd.DataFrame:
# Dropping duplicate salaries from table
df = employee[['salary']].drop_duplicates()

# Returning null if N is greater than number of salaries in table or if N is less than or equal to 0
if N > len(df) or N <= 0:
return pd.DataFrame({f'getNthHighestSalary({N})' : [None]})

# Ordering salaries in descending order and returning the N top ones. The tail function filters out the distinct Nth highest salary
return df.sort_values('salary', ascending = False).head(N).tail(1)[['salary']].rename(columns = {'salary':f'getNthHighestSalary({N})'})
11 changes: 11 additions & 0 deletions 02-SecondHighestSalary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Problem 2 - Second Highest Salary ( https://leetcode.com/problems/second-highest-salary/ )
import pandas as pd

def second_highest_salary(employee: pd.DataFrame) -> pd.DataFrame:
# Dropping duplicate salary values
df = employee[['salary']].drop_duplicates()
# Returning None if number of salaries is less than 2
if len(df) < 2:
return pd.DataFrame({'SecondHighestSalary' : [None]})
# Returning 2nd Highest Salary
return df.sort_values('salary', ascending = False).head(2).tail(1)[['salary']].rename(columns = {'salary':'SecondHighestSalary'})