How to drop rows from pandas data frame that contains a particular string in a particular column?
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Cool Puzzler LoFi
--
Chapters
00:00 How To Drop Rows From Pandas Data Frame That Contains A Particular String In A Particular Column?
00:23 Accepted Answer Score 311
00:42 Answer 2 Score 152
00:58 Answer 3 Score 55
01:16 Answer 4 Score 16
01:23 Thank you
--
Full question
https://stackoverflow.com/questions/2867...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas
#avk47
    Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Cool Puzzler LoFi
--
Chapters
00:00 How To Drop Rows From Pandas Data Frame That Contains A Particular String In A Particular Column?
00:23 Accepted Answer Score 311
00:42 Answer 2 Score 152
00:58 Answer 3 Score 55
01:16 Answer 4 Score 16
01:23 Thank you
--
Full question
https://stackoverflow.com/questions/2867...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas
#avk47
ACCEPTED ANSWER
Score 311
pandas has vectorized string operations, so you can just filter out the rows that contain the string you don't want:
In [91]: df = pd.DataFrame(dict(A=[5,3,5,6], C=["foo","bar","fooXYZbar", "bat"]))
In [92]: df
Out[92]:
   A          C
0  5        foo
1  3        bar
2  5  fooXYZbar
3  6        bat
In [93]: df[~df.C.str.contains("XYZ")]
Out[93]:
   A    C
0  5  foo
1  3  bar
3  6  bat
ANSWER 2
Score 152
If your string constraint is not just one string you can drop those corresponding rows with:
df = df[~df['your column'].isin(['list of strings'])]
The above will drop all rows containing elements of your list
ANSWER 3
Score 55
This will only work if you want to compare exact strings. It will not work in case you want to check if the column string contains any of the strings in the list.
The right way to compare with a list would be :
searchfor = ['john', 'doe']
df = df[~df.col.str.contains('|'.join(searchfor))]
ANSWER 4
Score 16
new_df = df[df.C != 'XYZ']
Reference: https://chrisalbon.com/python/data_wrangling/pandas_dropping_column_and_rows/