简体   繁体   English

如何比较两个不同数据帧的列并保持公共值

[英]How to compare columns of two different data frames and keep the common values

I have two data frames with same column but different values, out of which some are same and some are different. 我有两个具有相同列但值不同的数据框,其中有些是相同的,有些是不同的。 I want to compare both columns and keep the common values. 我想比较两列并保留通用值。

df1 : df1

  A B C
  1 1 1
  2 4 6
  3 7 9
  4 9 0
  6 0 1

df2 : df2

  A D E
  1 5 7
  5 6 9
  2 3 5
  7 6 8
  3 7 0

This is what I am expecting after comparison 这是我期望比较后的结果

df2 : df2

  A D E
  1 5 7
  2 3 5
  3 7 0

You can use pd.Index.intersection() to find the matching columns and do a inner merge finally reindex() to keep df2.columns : 您可以使用pd.Index.intersection()查找匹配的列,并最终进行内部合并reindex()来保持df2.columns

match=df2.columns.intersection(df1.columns).tolist() #finds matching cols in both df
df2.merge(df1,on=match).reindex(df2.columns,axis=1) #merge and reindex to df2.columns

   A  D  E
0  1  5  7
1  2  3  5
2  3  7  0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM