Skip to content Skip to sidebar Skip to footer

Is There A Way To Compare The Values Of A Pandas Dataframe With The Values Of A Second Dataframe?

I have 2 Pandas Dataframes with 5 columns and about 1000 rows each (working with python3). I'm interested in making a comparison between the first column in df1 and the first colum

Solution 1:

You will need to join your keywords in df2 first if you want to use str.contains method.

import pandas as pd

df = pd.DataFrame({'col1': {0: 'foobar', 1: 'acksyn', 2: 'foobaz', 3: 'ackfin'}})
df2 = pd.DataFrame({'col1': {0: 'old', 1: 'fin', 2: 'new', 3: 'bar'}})

print (df["col1"].str.contains("|".join(df2["col1"])))

#0True1False2False3True

Solution 2:

Possible Solution

"" for each row of DF1, if DF1.col1 ends in any values of DF2.col1, drop the row.""

This is a one-liner if I understand properly:

# Search for Substring# Generate an "OR" statement with a join# Drop if match. 
df[~df.col1.str.contains('|'.join(df2.col1.values))]

This will keep only the rows where DF2.Col1 is NOT found in DF1.Col1.

pd.Series.str.contains

Solution 3:

Take your frames

frame1 =frame1=pd.DataFrame({"col1":["foobar","acksyn","foobaz","ackfin"]})
frame2=pd.DataFrame({"col1":["old","fin","new","bar"]})

Then

myList=frame2.col2.values
pattern='|'.join(myList)

Finally

 frame1["col2"]=frame1["col1"].str.contains(pattern)
frame1.loc[frame1["col2"]==True]
     col1  col2
0  foobar  True3  ackfin  True

Post a Comment for "Is There A Way To Compare The Values Of A Pandas Dataframe With The Values Of A Second Dataframe?"