Skip to content
Advertisement

Check if a row in one DataFrame exist in another, BASED ON SPECIFIC COLUMNS ONLY

I have two Pandas DataFrame with different columns number.

df1 is a single row DataFrame:

       a   X0   b     Y0    c

0    233  100  56  shark  -23

df2, instead, is multiple rows Dataframe:

         d   X0   e   f     Y0   g    h

0     snow  201  32  36    cat  58  336
1     rain  176  99  15  tiger  63  845
2      sun  193  81  42    dog  48  557
3    storm  100  74  18  shark  39  673     # <-- This row
4    cloud  214  56  27   wolf  66  406

I would to verify if the df1’s row is in df2, but considering X0 AND Y0 columns only, ignoring all other columns. In this example the df1’s row match the df2’s row at index 3, that have 100 in X0 and ‘shark’ in Y0.

The output for this example is:

True

Note: True/False as output is enough for me, I don’t care about index of matched row.

I founded similar questions but all of them check the entire row…

Advertisement

Answer

duplicated

df2.append(df1).duplicated(['X0', 'Y0']).iat[-1]

True

Save a tad bit of time

df2[['X0', 'Y0']].append(df1[['X0', 'Y0']]).duplicated().iat[-1]
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement