1

I'm trying to get python to loop through all of the rows in my pandas dataframe.x column and return True if this value is anywhere in the dataframe.y column.

I've already tried to do it like this, but it isn't working.

#Create a column for storage
df["y_exists"] = ""

#Here is a loop that always returns FALSE
if df.x in df.y:
    df.x_exists="TRUE"
else:
    df.x_exists="FALSE"

This just results in a column full of FALSE when I know that many values should have returned TRUE.

Ideally, if row one of the dataframe had a value of "105" in column x it would search for "105" in column y and return TRUE if it was there and FALSE if it was not and recored this into the new column I created.

Churly Pickle
  • 323
  • 1
  • 7
  • 15

1 Answers1

4

You are looking for pandas.DataFrame.isin.

However, you shouldn't loop but use vectorization instead, as it is more efficient, like this:

df['exists_in_y']=df['x'].isin(df['y'])
Ricky Kim
  • 1,992
  • 1
  • 9
  • 18