Home > database >  Pandas multiconditional drop using two dataframes
Pandas multiconditional drop using two dataframes

Time:01-11

How can I drop some values from a dataframe using another dataframe as parameter?

df1
code | reply
A1   | yes
A2   | yes
A3   | no

df2
code |
A1   |
A1   |
A3   |

df_new = df1.drop(df1['code'] == df2['code'] & df1['reply'] != 'yes')

df_new
code | reply
A1   | yes

Is there a simple way to do this using .drop()?

CodePudding user response:

Use boolean indexing:

out = df1[df1['code'].isin(df2['code']) & df1['reply'].eq('yes')]

Output:

  code reply
0   A1   yes

CodePudding user response:

The logic is unclear but you can accomplish what you want without the use of drop:

>>> df1[df1['reply'] == 'yes'].merge(df2.drop_duplicates('code'))

  code reply
0   A1   yes
  • Related