Skip to main content
 首页 » 编程设计

python之Pandas 删除部分重复项

2025年05月04日20虾米哥

我有 2 个 dfs:

df1:

    x  y  z 
0   1  2  r 
1   a  c  2 
2  22  g  d 

df2:

    x  y  z 
0   1  2  r 
1   a  b  2 
2   3  g  d 

我想在列 y 和 z 重复时删除。

期望的结果:

        x  y  z 
    1   a  c  2 

因为 df1 和 df2 在 y 和 z 列中具有相同的值

请您参考如下方法:

 cols=['y','z']#columns to check for having same value 
 df1[~(df1[cols]==df2[cols]).all(axis=1)]#extracting the rows where x and y are `not equal(~)` in both dataframes