Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 638d7eb

Browse files
feat: add pandas solution to lc problem: No.0184 (doocs#4331)
1 parent 3bdf01e commit 638d7eb

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

‎solution/0100-0199/0184.Department Highest Salary/README.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,31 @@ WHERE
114114
);
115115
```
116116

117+
### Pandas
118+
119+
```python
120+
import pandas as pd
121+
122+
123+
def department_highest_salary(
124+
employee: pd.DataFrame, department: pd.DataFrame
125+
) -> pd.DataFrame:
126+
# Merge the two tables on departmentId and department id
127+
merged = employee.merge(department, left_on='departmentId', right_on='id')
128+
129+
# Find the maximum salary for each department
130+
max_salaries = merged.groupby('departmentId')['salary'].transform('max')
131+
132+
# Filter employees who have the highest salary in their department
133+
top_earners = merged[merged['salary'] == max_salaries]
134+
135+
# Select required columns and rename them
136+
result = top_earners[['name_y', 'name_x', 'salary']].copy()
137+
result.columns = ['Department', 'Employee', 'Salary']
138+
139+
return result
140+
```
141+
117142
<!-- tabs:end -->
118143

119144
<!-- solution:end -->

‎solution/0100-0199/0184.Department Highest Salary/README_EN.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,31 @@ WHERE
116116
);
117117
```
118118

119+
### Pandas
120+
121+
```python
122+
import pandas as pd
123+
124+
125+
def department_highest_salary(
126+
employee: pd.DataFrame, department: pd.DataFrame
127+
) -> pd.DataFrame:
128+
# Merge the two tables on departmentId and department id
129+
merged = employee.merge(department, left_on='departmentId', right_on='id')
130+
131+
# Find the maximum salary for each department
132+
max_salaries = merged.groupby('departmentId')['salary'].transform('max')
133+
134+
# Filter employees who have the highest salary in their department
135+
top_earners = merged[merged['salary'] == max_salaries]
136+
137+
# Select required columns and rename them
138+
result = top_earners[['name_y', 'name_x', 'salary']].copy()
139+
result.columns = ['Department', 'Employee', 'Salary']
140+
141+
return result
142+
```
143+
119144
<!-- tabs:end -->
120145

121146
<!-- solution:end -->
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pandas as pd
2+
3+
4+
def department_highest_salary(
5+
employee: pd.DataFrame, department: pd.DataFrame
6+
) -> pd.DataFrame:
7+
# Merge the two tables on departmentId and department id
8+
merged = employee.merge(department, left_on='departmentId', right_on='id')
9+
10+
# Find the maximum salary for each department
11+
max_salaries = merged.groupby('departmentId')['salary'].transform('max')
12+
13+
# Filter employees who have the highest salary in their department
14+
top_earners = merged[merged['salary'] == max_salaries]
15+
16+
# Select required columns and rename them
17+
result = top_earners[['name_y', 'name_x', 'salary']].copy()
18+
result.columns = ['Department', 'Employee', 'Salary']
19+
20+
return result

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /