Lowest Earning Employees
EasyPremium
Given the schema shown below, write a solution to fetch the 3 lowest-earning employees, including their IDs, names and salaries.
employees +---------------+---------+ | id | int | | first_name | varchar | | last_name | varchar | | salary | int | | department_id | int | +---------------+---------+
Your query should output a result in the following format:
id | first_name | last_name | salary ----+------------+-----------+-------- int | varchar | varchar | int
This question tests your understanding of a few basic dataframe manipulation concepts like sorting and limiting. Here's our complete solution:
Pythondef lowest_earning_employees(employees: pd.DataFrame) -> pd.DataFrame:
# Select relevant columns
selected_columns = employees[['id', 'first_name', 'last_name', 'salary']]
# Sort by salary in ascending order
sorted_employees = selected_columns.sort_values(by='salary', ascending=True)
# Limit to the first 3 entries (which are the lowest 3 salaries)
lowest_earning_employees = sorted_employees.head(3)
return lowest_earning_employees
def lowest_earning_employees(employees: pd.DataFrame) -> pd.DataFrame: selected_columns = employees[['id','first_name','last_name','salary' ]] sorted_df = selected_columns.sort_values(by='salary', ascending=True) return sorted_df.head(3)