看过来
《pandas 教程》 持续更新中,提供建议、纠错、催更等加作者微信: gr99123(备注:pandas教程)和关注公众号「盖若」ID: gairuo。跟作者学习,请进入 Python学习课程。欢迎关注作者出版的书籍:《深入浅出Pandas》 和 《Python之光》。
(编码题)人事部有两张表:
emp
:员工基础信息,已设置工号为索引dept
:部门信息,同样以工号为索引import pandas as pd
emp = pd.DataFrame(
{"姓名": ["张三", "李四", "王五"]},
index=pd.Index(["E001", "E002", "E003"], name="工号")
)
dept = pd.DataFrame(
{"部门": ["技术部", "人事部"]},
index=pd.Index(["E001", "E003"], name="工号")
)
用 一行 emp.join(dept)
完成 左连接(以 emp
为主),补全部门信息,缺失填 NaN
,并打印结果。
代码如下:
full = emp.join(dept)
print(full)
输出:
姓名 部门
工号
E001 张三 技术部
E002 李四 NaN
E003 王五 人事部
(完)
更新时间:2025-08-28 10:50:38 标签:pandas python 左连接 补全