Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Pandas solution for 184. Department Highest Salary #4331

Merged
merged 4 commits into from
Apr 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions solution/0100-0199/0184.Department Highest Salary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,31 @@ WHERE
);
```

### Pandas

```python
import pandas as pd


def department_highest_salary(
employee: pd.DataFrame, department: pd.DataFrame
) -> pd.DataFrame:
# Merge the two tables on departmentId and department id
merged = employee.merge(department, left_on='departmentId', right_on='id')

# Find the maximum salary for each department
max_salaries = merged.groupby('departmentId')['salary'].transform('max')

# Filter employees who have the highest salary in their department
top_earners = merged[merged['salary'] == max_salaries]

# Select required columns and rename them
result = top_earners[['name_y', 'name_x', 'salary']].copy()
result.columns = ['Department', 'Employee', 'Salary']

return result
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
25 changes: 25 additions & 0 deletions solution/0100-0199/0184.Department Highest Salary/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,31 @@ WHERE
);
```

### Pandas

```python
import pandas as pd


def department_highest_salary(
employee: pd.DataFrame, department: pd.DataFrame
) -> pd.DataFrame:
# Merge the two tables on departmentId and department id
merged = employee.merge(department, left_on='departmentId', right_on='id')

# Find the maximum salary for each department
max_salaries = merged.groupby('departmentId')['salary'].transform('max')

# Filter employees who have the highest salary in their department
top_earners = merged[merged['salary'] == max_salaries]

# Select required columns and rename them
result = top_earners[['name_y', 'name_x', 'salary']].copy()
result.columns = ['Department', 'Employee', 'Salary']

return result
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
20 changes: 20 additions & 0 deletions solution/0100-0199/0184.Department Highest Salary/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pandas as pd


def department_highest_salary(
employee: pd.DataFrame, department: pd.DataFrame
) -> pd.DataFrame:
# Merge the two tables on departmentId and department id
merged = employee.merge(department, left_on='departmentId', right_on='id')

# Find the maximum salary for each department
max_salaries = merged.groupby('departmentId')['salary'].transform('max')

# Filter employees who have the highest salary in their department
top_earners = merged[merged['salary'] == max_salaries]

# Select required columns and rename them
result = top_earners[['name_y', 'name_x', 'salary']].copy()
result.columns = ['Department', 'Employee', 'Salary']

return result