当前位置: 首页 > article >正文

力扣:184. 部门工资最高的员工(Python3)

题目:

表: Employee

+--------------+---------+
| 列名          | 类型    |
+--------------+---------+
| id           | int     |
| name         | varchar |
| salary       | int     |
| departmentId | int     |
+--------------+---------+
在 SQL 中,id是此表的主键。
departmentId 是 Department 表中 id 的外键(在 Pandas 中称为 join key)。
此表的每一行都表示员工的 id、姓名和工资。它还包含他们所在部门的 id。

表: Department

+-------------+---------+
| 列名         | 类型    |
+-------------+---------+
| id          | int     |
| name        | varchar |
+-------------+---------+
在 SQL 中,id 是此表的主键列。
此表的每一行都表示一个部门的 id 及其名称。

查找出每个部门中薪资最高的员工。
按 任意顺序 返回结果表。
查询结果格式如下例所示。

来源:力扣(LeetCode)
链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

示例:

示例 1:

输入:

Employee 表:
+----+-------+--------+--------------+
| id | name  | salary | departmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  | 1            |
| 2  | Jim   | 90000  | 1            |
| 3  | Henry | 80000  | 2            |
| 4  | Sam   | 60000  | 2            |
| 5  | Max   | 90000  | 1            |
+----+-------+--------+--------------+
Department 表:
+----+-------+
| id | name  |
+----+-------+
| 1  | IT    |
| 2  | Sales |
+----+-------+


输出:

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Jim      | 90000  |
| Sales      | Henry    | 80000  |
| IT         | Max      | 90000  |
+------------+----------+--------+


解释:Max 和 Jim 在 IT 部门的工资都是最高的,Henry 在销售部的工资最高。

解法:

将Employee表左外连接Department表,根据部门分组找出每个部门的最高薪水。由于最高薪水对应的人数可能不止1人,所以遍历合并后的表,取出每个部门对应最高薪水的人的信息。

知识点:

1.DataFrame.itertuples(self, index=True, name='Pandas') :返回一个迭代器,为DataFrame中的每行生成一个命名的元组。元组的第1个元素将是行的相应索引值,而其余值是行值。index:是否将将索引作为元组的第1个元素返回,默认为True;name:返回的namedtuple的名称。例如:

data = [[1, 'Joe', 70000, 1], [2, 'Jim', 90000, 1], [3, 'Henry', 80000, 2], [4, 'Sam', 60000, 2], [5, 'Max', 90000, 1]]
employee = pd.DataFrame(data, columns=['id', 'name', 'salary', 'departmentId']).astype({'id': 'Int64', 'name': 'object', 'salary': 'Int64', 'departmentId': 'Int64'})
for item in employee.itertuples():
    print(item)

2.getattr(object, name[, default]):返回对象属性值。object:对象,name:对象属性名。

代码:

import pandas as pd
from collections import defaultdict


def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame:
    dic = defaultdict(list)
    m = pd.merge(employee, department, how='left', left_on='departmentId', right_on='id')[['name_y', 'name_x', 'salary']].sort_values(['name_y', 'salary'], ascending=[True, False], ignore_index=True)
    for item in m.itertuples():
        if len(dic[getattr(item, 'name_y')]) == 0 or getattr(item, 'salary') == dic[getattr(item, 'name_y')][0][0]:
            dic[getattr(item, 'name_y')].append([getattr(item, 'salary'), getattr(item, 'Index')])
    rows = []
    for item in list(map(lambda x: list(zip(*x))[1], dic.values())):
        rows.extend(item)
    return m.iloc[rows].rename(columns={'name_y': 'Department', 'name_x': 'Employee'})


http://www.kler.cn/news/150073.html

相关文章:

  • python getattr() setattr() hasattr() delattr()内置函数详解
  • 智慧博物馆视频监控系统设计,可视化AI智能分析技术助力博物馆多维度监管
  • SparkContext初始化
  • 错误 LNK2001 无法解析的外部符号 __imp__CrtDbgReport
  • 短 URL 生成器设计:百亿短 URL 怎样做到无冲突?
  • 2023.11.28 MyBatis 中 #{} 和 ${} 的区别
  • 【ZEDSLAM】Ubuntu18.04系统ZED 2i双目相机SDK安装、联合标定、SLAM测试
  • 离散化笔记
  • 在与客户打交道过程中为什么客户不信任你?
  • 阿里云语雀频繁崩溃,有什么文档管理工具是比较稳定的?
  • 在虚拟机搭建nignx,和使用本地访问nginx的情况
  • viple模拟器使用(三):unity模拟器中实现沿右墙迷宫算法
  • C/C++ Zlib实现文件压缩与解压
  • 集合的使用
  • leetcode:随机链表的复制
  • 【Python】获取ip
  • NTT 的各类优化:Harvey、PtNTT,Intel AVX2、ARM Neon、GPGPU
  • oracle的sysaux使用量排查sql
  • 【ChatGLM3-6B】Docker下部署及微调
  • 6.golang函数
  • C语言变量和常量
  • Veras:Revit AI渲染插件
  • Mybatis 使用枚举作为查询条件
  • Linux:Ubuntu系统安装软件
  • 【Spring之事务底层源码解析,持续更新中~~~】
  • Elasticsearch:向量搜索 (kNN) 实施指南 - API 版
  • 使用Java给钉钉群发消息
  • 【小聆送书第一期】让架构师的成神之路温暖你这个不景气的冬天
  • GPTS-生成一个动漫图像GPT
  • 2023-简单点-picamera2中的取消auto focus,进行手动焦距设定