Home > other >  Deleting rows in Pandas Dataframe, when column values match tuples in a list
Deleting rows in Pandas Dataframe, when column values match tuples in a list

Time:12-08

I have a data frame

 index  col1  col2 col3
     0     1     3    5
     1    12     7   21
  ...    ...   ...  ...

I want to delete some rows, with the criteria being that the values in col1 and col2 show up in a certain list. Let the list be [(12,7),(100,34),...]. In this case, the row with index 1 would be deleted.

CodePudding user response:

Use Index.isin for test MultiIndex created by both columns by DataFrame.set_index, invert mask by ~ and filter in boolean indexing:

L = [(12,7),(100,34)]

df = df[~df.set_index(['col1','col2']).index.isin(L)]
print (df)
   col1  col2  col3
0     1     3     5
  • Related