diff --git a/Problem 1.py b/Problem 1.py new file mode 100644 index 0000000..8ea432d --- /dev/null +++ b/Problem 1.py @@ -0,0 +1,16 @@ +# Nth Highest Salary + +import pandas as pd + +def nth_highest_salary(employee: pd.DataFrame, N: int) -> pd.DataFrame: + df = employee.drop_duplicates().sort_values(by='salary', ascending=False) + if N > len(df): + return pd.DataFrame({f'getNthHighestSalary({N})': [None]}) + df = df[N - 1:N]['salary'] + df = pd.DataFrame(df).rename(columns={'salary': f'getNthHighestSalary({N})'}) + print(df) + return df + +data = [[1, 100], [2, 200], [3, 300]] +employee = pd.DataFrame(data, columns=['Id', 'salary']).astype({'Id':'Int64', 'salary':'Int64'}) +nth_highest_salary(employee, N=2) \ No newline at end of file diff --git a/Problem 2.py b/Problem 2.py new file mode 100644 index 0000000..5836a40 --- /dev/null +++ b/Problem 2.py @@ -0,0 +1,17 @@ +# Second Highest Salary + +import pandas as pd + +def second_highest_salary(employee: pd.DataFrame) -> pd.DataFrame: + df = employee.sort_values('salary', ascending=False).drop_duplicates() + if len(df) < 2: + df = pd.DataFrame({'SecondHighestSalary': [None]}) + else: + df = df.head(2).tail(1)['salary'] + df = pd.DataFrame(df).rename(columns={'salary': 'SecondHighestSalary'}) + print(df) + return df + +data = [[1, 100], [2, 200], [3, 300]] +employee = pd.DataFrame(data, columns=['id', 'salary']).astype({'id':'int64', 'salary':'int64'}) +second_highest_salary(employee) \ No newline at end of file