GuillermoDC
GuillermoDC

Reputation: 51

Pandas - drop rows based on two conditions on different columns

Although there are several related questions answered in Pandas, I cannot solve this issue. I have a large dataframe (~ 49000 rows) and want to drop rows the meet two conditions at the same time(~ 120):

My code is ignoring the conditions and no row is removed.

to_remove = ['string1', 'string2'] 
df.drop(df[df['Column 1'].isin(to_remove) & (df['Column 2'].isna())].index, inplace=True)

What am I doing wrong? Thanks for any hint!

Upvotes: 1

Views: 1917

Answers (1)

ThePyGuy
ThePyGuy

Reputation: 18406

Instead of calling drop, and passing the index, You can create the mask for the condition for which you want to keep the rows, then take only those rows. Also, the logic error seems to be there, you are checking two different condition combined by AND for the same column values.

df[~(df['Column1'].isin(to_remove) & (df['Column2'].isna()))]

Also, if you need to check in the same column, then you probably want to combine the conditions by or i.e. | If needed, you can reset_index at last.

Also, as side note, your list to_remove has two same string values, I'm assuming thats a typo in the question.

Upvotes: 3

Related Questions