File tree Expand file tree Collapse file tree 3 files changed +70
-0
lines changed
solution/0100-0199/0184.Department Highest Salary Expand file tree Collapse file tree 3 files changed +70
-0
lines changed Original file line number Diff line number Diff 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 -->
Original file line number Diff line number Diff 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 -->
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments