🏆🏆Beats 97%| 📈📈Easy Solution |Python Pandas
\n\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n\uD83E\uDD14When working with organizational data, it\'s common to analyze empl
![Screenshot 2024-06-09 132228.png](https://assets.leetcode.com/users/images/0e802d6e-0feb-4328-8b25-05261abb4106_1717919633.5386968.png)\n\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\uD83E\uDD14When working with organizational data, it\'s common to analyze employee salaries to identify the highest earners in each department. This task involves joining data from two related tables: one containing employee details and another containing department information. By merging these tables and using grouping and filtering techniques, we can extract the top earners for each department. \uD83D\uDCCA\uD83D\uDCBC\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1) **Data Merging** \uD83D\uDD17:\nCombine the Employee and Department tables using a join operation on the departmentId and id columns.\n2) **Grouping and Aggregation** \uD83D\uDCCA:\nGroup the merged data by departmentId and find the maximum salary within each group.\n3) **Filtering Top Earners** \uD83C\uDFC6:\nFilter the merged data to retain only the rows where the salary matches the highest salary in each department.\n4) **Result Formatting** \uD83D\uDCCB:\nSelect and rename the relevant columns to match the desired output format.\n\n\n# Code\n```\nimport pandas as pd\n\ndef department_highest_salary(df1: pd.DataFrame, df2: pd.DataFrame) -> pd.DataFrame:\n    df = pd.merge(left=df1,right=df2,left_on=\'departmentId\',right_on=\'id\',how=\'left\')\n    bf = df.groupby(\'departmentId\').max()\n    pf = pd.merge(left=df,right=bf,on=[\'salary\',\'name_y\'])\n    return pf[[\'name_y\',\'name_x_x\',\'salary\']].rename(columns={\'name_y\':\'Department\',\'name_x_x\':\'Employee\',\'salary\':\'Salary\'})\n```
['Python3', 'Pandas']