From 4bd31cf1d8a50151673516e0176220af94899374 Mon Sep 17 00:00:00 2001 From: Radhika Tekade Date: Thu, 6 Mar 2025 10:51:30 -0800 Subject: [PATCH 1/2] Create 135_nth_highest_salary --- 135_nth_highest_salary.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 135_nth_highest_salary.py diff --git a/135_nth_highest_salary.py b/135_nth_highest_salary.py new file mode 100644 index 0000000..0513b12 --- /dev/null +++ b/135_nth_highest_salary.py @@ -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})'})) \ No newline at end of file From 30071c358d12823d40a9a8ca265d2f2f214c68ae Mon Sep 17 00:00:00 2001 From: Radhika Tekade Date: Thu, 6 Mar 2025 10:51:54 -0800 Subject: [PATCH 2/2] Create 136_second_highest_salary --- 136_second_highest_salary.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 136_second_highest_salary.py diff --git a/136_second_highest_salary.py b/136_second_highest_salary.py new file mode 100644 index 0000000..d7521d5 --- /dev/null +++ b/136_second_highest_salary.py @@ -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'})) \ No newline at end of file