简体   繁体   中英

Compare two colums in two pandas dataframes

I have two dataframes both containing a column name

Colums in dataframe 1 are ( idABT name description price)

Colums in dataframe 2 are ( idBuy name description manufacturer price)

There are some names in dataframe1 which are also found in dataframe2 how can i get th idABT and idBuy in another dataframe

Given two dataframes with the columns you describe,

import pandas
print(pandas.__version__)

df1 = pandas.DataFrame({'idABT':['A','F', 'H'], 
                        'name': ['B', 'G', 'I'], 
                        'description':['C','J','K'], 
                        'price':[1, 2, 3]})

df2 = pandas.DataFrame({'idBuy':['A', 'M','N'], 
                        'name': ['B', 'I', 'O'], 
                        'description':['C','P','Q'], 
                        'manufacturer':['E','X','Y'], 
                        'price':[5, 6, 7]})

You can merge the two dataframes using

df1.merge(df2, left_on='name', right_on='name')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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