简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM