Home > Mobile >  Two dataframes with 'isin()' only returning TRUE/FALSE
Two dataframes with 'isin()' only returning TRUE/FALSE

Time:03-15

After searching online and similar questions, it seems that there are many questions on this, but difficult to find a case that applies to my problem.

Columns of df_a contains many columns, one of which is x.
df_b also has the column x.

So I have the following code (and variations aa, bb, cc, dd -- all work)

aa = df_a['x'].isin(df_b['x'])
bb = df_a[['x']].isin(df_b['x'])
cc = df_a['x'].isin(df_b[['x']])
dd = df_a[['x']].isin(df_b[['x']])

Main problem: this only returns a vector of True/False. I would like for it to return all of df_a columns with only the rows filtered with True values. What do I need for this?

CodePudding user response:

IIUC use:

df1 = df[df_a.apply(lambda x: x.isin(df_b[x.name])).all(axis=1)]

Or maybe:

df2 = df1[df_a['x'].isin(df_b['x'])]

df2 = df1.merge(df2, on='x')

CodePudding user response:

Use:

aa = df_a[df_a['x'].isin(df_b['x'])].copy()
  • Related