查找两个数据框中第二列的值对于作为关键列的第一列已更改的行?

问题描述 投票:0回答:1

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
python pandas list-comprehension
1个回答
0
投票

如果我理解正确,您希望通过

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
© www.soinside.com 2019 - 2024. All rights reserved.