pythonpandas

Leetcode problem 176. Second Highest Salary


Table: Employee

+-------------+------+
| Column Name | Type |
+-------------+------+
| id          | int  |
| salary      | int  |
+-------------+------+

id is the primary key (column with unique values) for this table. Each row of this table contains information about the salary of an employee.

Write a solution to find the second highest distinct salary from the Employee table. If there is no second highest salary, return null (return None in Pandas). he result format is in the following example.

Example 1:

Input: Employee table:

+----+--------+
| id | salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

Output:

+---------------------+
| SecondHighestSalary |
+---------------------+
| 200                 |
+---------------------+

Example 2:

Input: Employee table:

+----+--------+
| id | salary |
+----+--------+
| 1  | 100    |
+----+--------+

Output:

+---------------------+
| SecondHighestSalary |
+---------------------+
| null                |
+---------------------+

In this problem I have attempt in pandas my code is like this:

import pandas as pd

def second_highest_salary(employee: pd.DataFrame) -> pd.DataFrame:
    employee.salary = employee.salary.drop_duplicates().reset_index(drop = True)
    employee = employee.sort_values(by = 'salary',ascending = False)
    if employee.shape[0] < 2:
        return pd.DataFrame(data = {'SecondHighestSalary':[None]})
    else:
        return pd.DataFrame(data = {'SecondHighestSalary':[employee.salary.iloc[[1]]]})

and for the test case

| id | salary |
| -- | ------ |
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |

the expected output is:

| SecondHighestSalary |
| ------------------- |
| 200                 |

and my output is also the same :

| SecondHighestSalary |
| ------------------- |
| 200                 |

I don't understand why is showing as a wrong answer but my output and expected output is similar. Please help me to understand the error.


Solution

  • iloc[[1]] already represents a DataFrame, so you should not wrap it in a list. Remove those wraping [ ] from your last return statement, so to get this:

    return pd.DataFrame(data = {'SecondHighestSalary':employee.salary.iloc[[1]]})