Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete row if any column contains one of the keywords

Tags:

I want to drop rows where any column contains one of the keywords

keywords=['Nokia' , 'Asus']

data = [['Nokia', 'AB123','broken'], ['iPhone', 'DF747','battery'], ['Acer', 'KH298','exchanged for a nokia'], ['Blackberry', 'jj091','exchanged for a Asus']] 
df = pd.DataFrame(data, columns = ['Brand', 'ID', 'Description']) 

df before:

Brand      | ID    |  Description
----------------------------------------
Nokia      | AB123 | broken
iPhone     | DF747 | battery
Acer       | KH298 | exchanged for a nokia
Blackberry | jj091 | exchanged for a Asus

df after:

Brand      | ID    |  Description
----------------------------------------
iPhone     | DF747 | battery
Acer       | KH298 | exchanged for a nokia

How can i achieve this?

like image 216
John Doe Avatar asked Sep 14 '19 08:09

John Doe


People also ask

How do I delete rows if a cell contains specific text in Excel?

To delete rows that contain these cells, right-click anywhere in the data range and from the drop-down menu, choose Delete.


1 Answers

You can join all columns together with + or apply and then create mask by Series.str.contains with joined values by | for regex OR:

df = df[~(df['Brand']+df['ID']+df['Description']).str.contains('|'.join(keywords))]

Or:

df = df[~df.apply(' '.join, 1).str.contains('|'.join(keywords))]
print (df)
    Brand     ID            Description
1  iPhone  DF747                battery
2    Acer  KH298  exchanged for a nokia

If need case not sensitive add case paremeter:

df = df[~df.apply(' '.join, 1).str.contains('|'.join(keywords), case=False)]
print (df)
    Brand     ID Description
1  iPhone  DF747     battery
like image 134
jezrael Avatar answered Dec 27 '22 11:12

jezrael