Python Forum

Full Version: Comparing Items Different Data Frames With a WHILE Loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all

I was wondering if anyone could help with comparing items in one data frame with an items in another data frame.

I have 2 x data frames
DF_One
DF_TWO

I want to loop through all of the values in DF_One in the first column and compare this with all of the values in the first column in DF_Two.

The Code would be like:-

Do until the end of DF_One is reached
...Current_Value = value in row (t),column 1 of DF_One
......Do until the end of DF_Two is reached
.........if Current_Value = row(i), Column 1 in DF_Two
............print ("found")
.........end if
......i=i+1
t=t+1

I want to use a while loop and not the where function.

Can anyone help or just point me in the right direction.

Thank you.
There is .isin method, which provides this functionality.
import pandas as pd
df1 = pd.DataFrame({'x': pd.np.random.randint(1, 1000, size=200)})
df2 = pd.DataFrame({'x': pd.np.random.randint(1, 1000, size=100)}) 
df1.x.isin(df2.x)
Pure Python loops will be very slow in case of large dfs.