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 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