Skip to content

Commit b211451

Browse files
committed
Add Pandas solution for department highest salary
1 parent cd51270 commit b211451

File tree

3 files changed

+50
-49
lines changed

3 files changed

+50
-49
lines changed

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -92,28 +92,6 @@ Department 表:
9292

9393
<!-- solution:start -->
9494

95-
### Python3
96-
97-
```python
98-
import pandas as pd
99-
100-
def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame:
101-
# Merge the two tables on departmentId and department id
102-
merged = employee.merge(department, left_on='departmentId', right_on='id')
103-
104-
# Find the maximum salary for each department
105-
max_salaries = merged.groupby('departmentId')['salary'].transform('max')
106-
107-
# Filter employees who have the highest salary in their department
108-
top_earners = merged[merged['salary'] == max_salaries]
109-
110-
# Select required columns and rename them
111-
result = top_earners[['name_y', 'name_x', 'salary']].copy()
112-
result.columns = ['Department', 'Employee', 'Salary']
113-
114-
return result
115-
```
116-
11795
### 方法一:等值连接 + 子查询
11896

11997
我们可以使用等值连接,将 `Employee` 表和 `Department` 表连接起来,连接条件为 `Employee.departmentId = Department.id`,然后使用子查询来找到每个部门的最高工资,最后使用 `WHERE` 子句来筛选出每个部门中薪资最高的员工。
@@ -136,6 +114,28 @@ WHERE
136114
);
137115
```
138116

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

141141
<!-- solution:end -->

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -94,28 +94,6 @@ Department table:
9494

9595
<!-- solution:start -->
9696

97-
### Python3
98-
99-
```python
100-
import pandas as pd
101-
102-
def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame:
103-
# Merge the two tables on departmentId and department id
104-
merged = employee.merge(department, left_on='departmentId', right_on='id')
105-
106-
# Find the maximum salary for each department
107-
max_salaries = merged.groupby('departmentId')['salary'].transform('max')
108-
109-
# Filter employees who have the highest salary in their department
110-
top_earners = merged[merged['salary'] == max_salaries]
111-
112-
# Select required columns and rename them
113-
result = top_earners[['name_y', 'name_x', 'salary']].copy()
114-
result.columns = ['Department', 'Employee', 'Salary']
115-
116-
return result
117-
```
118-
11997
### Solution 1: Equi-Join + Subquery
12098

12199
We can use an equi-join to join the `Employee` table and the `Department` table based on `Employee.departmentId = Department.id`, and then use a subquery to find the highest salary for each department. Finally, we can use a `WHERE` clause to filter out the employees with the highest salary in each department.
@@ -138,6 +116,28 @@ WHERE
138116
);
139117
```
140118

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

143143
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import pandas as pd
22

3+
34
def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame:
45
# Merge the two tables on departmentId and department id
56
merged = employee.merge(department, left_on='departmentId', right_on='id')
6-
7+
78
# Find the maximum salary for each department
89
max_salaries = merged.groupby('departmentId')['salary'].transform('max')
9-
10+
1011
# Filter employees who have the highest salary in their department
1112
top_earners = merged[merged['salary'] == max_salaries]
12-
13+
1314
# Select required columns and rename them
1415
result = top_earners[['name_y', 'name_x', 'salary']].copy()
1516
result.columns = ['Department', 'Employee', 'Salary']
16-
17-
return result
17+
18+
return result

0 commit comments

Comments
 (0)