我try 通过列cust_id和town_id将dataframe df1df2进行比较,并获得其town_id已更改的所有行pf cust_id.我可以使用列表理解来获得cust_id的列表,这些列表在df21中,但不在df2中,反之亦然.但是,如何使用town_id change查找已经更改了town_id的cust_id,并将输出生成为dataframe呢?

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

Python相关问答推荐

从多行文本中提取事件对

回归回溯-2D数组中的单词搜索

脚注在Python中使用regex导致错误匹配

将词典写入Excel

Flask主机持续 bootstrap 本地IP| Python

将列表中的元素替换为收件箱中的元素

如何编写一个正规表达式来查找序列中具有2个或更多相同辅音的所有单词

使用imap-tools时错误,其邮箱地址包含域名中的非默认字符

Python panda拆分列保持连续多行

如何使用entry.bind(FocusIn,self.Method_calling)用于使用网格/列表创建的收件箱

Python中的负前瞻性regex遇到麻烦

LAB中的增强数组

理解Python的二分库:澄清bisect_left的使用

Pandas 都是(),但有一个门槛

在pandas中使用group_by,但有条件

AES—256—CBC加密在Python和PHP中返回不同的结果,HELPPP

在Admin中显示从ManyToMany通过模型的筛选结果

在Docker容器(Alpine)上运行的Python应用程序中读取. accdb数据库

将链中的矩阵乘法应用于多组值

如何在Python 3.9.6和MacOS Sonoma 14.3.1下安装Pyregion