pandas find if an ID exist in another column

  Kiến thức lập trình

Imagine i have a dataframe with ID1 and ID2

import pandas as pd
 
# initialize list of lists
data = [[1, 10], [2, 15], [10, 14]]
 
# Create the pandas DataFrame
df_test = pd.DataFrame(data, columns=['ID1', 'ID2'])

I would like to find if ID1 exists in ID2

so in this case the output should be

ID1,ID2,Exists
1,10,False
2,15,False
10,14,True

I followed the answers here

Check if column value is in other columns in pandas

But the output is all True, which is not correct

df_test["exists"] = df_test.drop("ID1", 1).isin(df_test["ID2"]).any(1)

Could someone help? In the environment is Python 3. Thank you

1

You can use np.intersect1d:

intersection = np.intersect1d(df_test["ID1"], df_test["ID2"])
df_test["Exists"] = df_test["ID1"].isin(intersection)

print(df_test)

Prints:

   ID1  ID2  Exists
0    1   10   False
1    2   15   False
2   10   14    True

1

Most of the time I prefer the transform method.

df_test['exists'] = df_test['ID1'].transform(lambda x: x in df_test['ID2'].values)

LEAVE A COMMENT