1
我正在尝试按列 cust_id 和town_id 比较数据帧 df1 与 df2,并获取 Town_id 已更改的所有 pf cust_id 行。我可以使用列表理解来获取位于 df21 中但不在 df2 中的 cust_id 列表,反之亦然。但是如何使用town_id更改来查找town_id已更改的cust_id并生成输出作为数据帧?
df1
name cust_id town_id
1 cxa c1001 t001
2 cxb c1002 t001
3 cxc c1003 t001
4 cxd c1004 t002
df2
name cust_id town_id
1 cxa c1001 t002
2 cxb c1002 t001
3 cxd c1004 t001
4 cxe c1005 t001
5 cxf c1006 t001
output
name cust_id townId_initial town_id_latter
1 cxa c1001 t001 t002
2 cxd c1006 t002 t001
如果我理解正确,您希望通过
cust_id
合并两个数据帧,然后找到 town_id
不同的行:
out = df1.merge(df2, on="cust_id", how="inner", suffixes=["_initial", "_latter"])
out = out[out.town_id_initial != out.town_id_latter]
print(
out[["name_initial", "cust_id", "town_id_initial", "town_id_latter"]].rename(
columns={"name_initial": "name"}
)
)
打印:
name cust_id town_id_initial town_id_latter
0 cxa c1001 t001 t002
2 cxd c1004 t002 t001