techwreck
techwreck

Reputation: 53

How to delete a certain row if it contains a particular string in it using python?

I have to create a clean list wherein names with 'Trust' or 'Trustee' in rows get deleted.

I'm using the following code but i'm not getting the desired result ?

df_clean = df[~df['Row Labels'].str.contains('trusteeship')]

eg : if the 'Row Labels' contains a row with ABC Trust or XYTrusteeshipZ, then the whole row should get deleted.

df_clean = df[~df['Row Labels'].str.contains('Trust')]

df_clean = df[~df['Row Labels'].str.lower().str.contains('trust')]

Upvotes: 1

Views: 57

Answers (1)

jezrael
jezrael

Reputation: 863216

You can match with case=False parameter for ignore lower/uppercase characters:

df_clean = df[~df['Row Labels'].str.contains('trust', case=False)]

Or first convert values to lowercase like mentioned @anon01 in comments:

df_clean = df[~df['Row Labels'].str.lower().str.contains('trust')]

Upvotes: 1

Related Questions