N-ésimo Salario más alto
Tabla: Employee
Escribe una solución para encontrar el salario distinto más alto de la tabla Employee. Si hay menos de n salarios distintos, retorna null
Ejemplos
Input:
Employee table: +----+--------+ | id | salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ n = 2Output:
+------------------------+ | getNthHighestSalary(2) | +------------------------+ | 200 | +------------------------+Ejemplo 2:
Input:
Employee table: +----+--------+ | id | salary | +----+--------+ | 1 | 100 | +----+--------+ n = 2Output:
+------------------------+ | getNthHighestSalary(2) | +------------------------+ | null | +------------------------+Soluciones
Python
import pandas as pd
def nth_highest_salary(employee: pd.DataFrame, N: int) -> pd.DataFrame:
if N <= 0:
result = None
else:
unique_salaries = employee['salary'].drop_duplicates().sort_values(ascending=False)
if len(unique_salaries) >= N:
result = unique_salaries.iloc[N-1]
else:
result = None
return pd.DataFrame({f'getNthHighestSalary({N})': [result]})