Average Compensation by Title and Gender
Problem
**[Classic — multi-table HR analytics]**
Three tables: `employees` (with `title_id`, `gender`), `titles`
(with `title_name`), and `compensation` (with `base_salary`, `bonus`).
For each combination of title and gender, compute the **average total
compensation** (base + bonus).
Return `title_name`, `gender`, `avg_compensation`, ordered by title then gender.
Input data
Example rows — the live problem includes the full dataset.
employees
| employee_id | name | title_id | gender |
|---|---|---|---|
| 1 | Alice | 1 | F |
| 2 | Bob | 1 | M |
| 3 | Carol | 2 | F |
| 4 | Dave | 2 | M |
| 5 | Eve | 1 | F |
titles
| title_id | title_name |
|---|---|
| 1 | Engineer |
| 2 | Manager |
compensation
| employee_id | base_salary | bonus |
|---|---|---|
| 1 | 80000 | 5000 |
| 2 | 85000 | 6000 |
| 3 | 100000 | 10000 |
| 4 | 105000 | 12000 |
| 5 | 78000 | 4000 |
Expected output
Your answer should return 4 rows with the columns title_name, gender, avg_compensation.
Starter code (Pandas (Python))
import pandas as pd
def avg_compensation(employees: pd.DataFrame, titles: pd.DataFrame, compensation: pd.DataFrame) -> pd.DataFrame:
# Your code here
return employeesSolve this Pandas question free
Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock