简体   繁体   English

查找两个数据框中的共同元素

[英]Find common elements in two dataframes

I have two dataframes df1 and df2 having the same columns.我有两个数据df1df2具有相同的列。 I would like to find the elements within a column that are in common.我想在列中找到共同的元素。 For example例如

df1 : df1

Col1 Col2 Nam1 Nam2 Net
AD    AS   AS   ADS AB
BF    SA   WQ   AFW AF
RW    KJ   IQ   QIE LK

df2 : df2

Col1 Col2 Nam1 Nam2 Net
RW    WQ   HF   HGJ AB
BF    AS   DD   VCC LJ
RW    KJ   IQ   ADS JH
DS    QW   LJ   NB  LK

I would like to have the following (on Net)我想要以下(在网上)

Col1 Col2 Nam1 Nam2 Net
AD    AS   AS   ADS AB
RW    WQ   HF   HGJ AB
RW    KJ   IQ   QIE LK
DS    QW   LJ   NB  LK

I have tried as follows:我试过如下:

df=pd.merge(df1, df2, on='Net', how='inner')

but it duplicates all the columns' name (_x and _y) and also it seems not extracting only the elements in common.但它重复了所有列的名称(_x 和 _y),而且它似乎没有只提取共同的元素。

IIUC, you just want those rows which share the same net value? IIUC,您只想要那些共享相同净值的行吗? You can start with,你可以从,

vals =  set(df1['Net']).intersection(df2['Net']) 
print (vals)
# {'AB', 'LK'}

Now, filter out those values and concatenate:现在,过滤掉这些值并连接:

pd.concat([
    df1.query('Net in @vals'),   
    df2.query('Net in @vals')], ignore_index=True)

  Col1 Col2 Nam1 Nam2 Net
0   AD   AS   AS  ADS  AB
1   RW   KJ   IQ  QIE  LK
2   RW   WQ   HF  HGJ  AB
3   DS   QW   LJ   NB  LK

One explicit way to do it would be:一种明确的方法是:

  1. Obtain set of common items获取常用项集
common_items = set(df1['Net']) & set(df2['Net'])
  1. Get relevant rows from dataframes从数据框中获取相关行
df1_common = df1[df1['Net'].isin(common_items)]

Col1 Col2 Nam1 Nam2 Net
AD    AS   AS   ADS AB
RW    KJ   IQ   QIE LK
df2_common = df2[df2['Net'].isin(common_items)] 

Col1 Col2 Nam1 Nam2 Net
RW    WQ   HF   HGJ AB
DS    QW   LJ   NB  LK
  1. Concatenate the two dataframes连接两个数据框
pd.concat([df1_common, df2_common])
    
Col1 Col2 Nam1 Nam2 Net
AD    AS   AS   ADS AB
RW    WQ   HF   HGJ AB
RW    KJ   IQ   QIE LK
DS    QW   LJ   NB  LK

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

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