0

This is what I have

df1:
    name
----------------
0  dog  
1  cat
2  chicken

df2
   name
---------
0  dog-collie
1  cat
2  chicken

what I'm trying to get is that compare first df1['name'] with df2['name'] to filter dog-collie

the result

df1
   name
-----------
0  cat
1  chicken 

I could compare both column and add an extra column with 'True' or 'False' after comparison,like this

df1['result']=df1['name']==df2['name']
df1=df1['result']==True

but I wonder if there is any other way to do this (possibly a faster way, since there are a lot of rows)

thanks

ikel
  • 1,790
  • 6
  • 31
  • 61

1 Answers1

0

You can accomplish this easily by using the DataFrame.merge method to compute the intersection of df1.name and df2.name.

result = df1.merge(df2, how='inner', on='name')
# result is a DataFrame containing only the matching names from the original dataframes

I'm not sure about the implementation details of merge, but I imagine it has either similar or better efficiency than your solution.

zachdj
  • 354
  • 2
  • 8