Home > Enterprise >  How to delete rows based on two fields?
How to delete rows based on two fields?

Time:10-12

I have a df with lots of ids and dates, I need to delete from this df rows with id = 4 where date != '2021-01-01' This expression, I assume won't work

df_2 = df_2[df_2['id'] != 4 & df_2['date'] != '2021-01-01']

How else can I write the condition?

E.g.

4 2020-01-01
5 2021-05-01
4 2021-01-01
4 2021-09-01

Should become

5 2021-05-01
4 2021-01-01

CodePudding user response:

Add parantheses and chain mask by | for bitwise OR and swap == with !=:

df_2 = df_2[(df_2['id'] != 4) | (df_2['date'] == '2021-01-01')]
print (df_2)
   id        date
1   5  2021-05-01
2   4  2021-01-01

Your solution should be change with invert mask by ~:

df_2 = df_2[ ~((df_2['id'] == 4) & (df_2['date'] != '2021-01-01'))]
  • Related