Partial Match If Statement Pandas
--------------------------------------------------
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: Drifting Through My Dreams
--
Chapters
00:00 Partial Match If Statement Pandas
00:37 Accepted Answer Score 4
00:52 Answer 2 Score 3
01:20 Thank you
--
Full question
https://stackoverflow.com/questions/4225...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas #ifstatement
#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: Drifting Through My Dreams
--
Chapters
00:00 Partial Match If Statement Pandas
00:37 Accepted Answer Score 4
00:52 Answer 2 Score 3
01:20 Thank you
--
Full question
https://stackoverflow.com/questions/4225...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas #ifstatement
#avk47
ACCEPTED ANSWER
Score 4
Using str.contains
In [832]: df.Title.str.contains('Dog')
Out[832]:
0 True
1 False
2 True
Name: Title, dtype: bool
In [833]: df['Match'] = df.Title.str.contains('Dog')
In [834]: df
Out[834]:
Title Author Name Match
0 Dogs R Us John Smith True
1 Pigs can Fly Henry White False
2 Dog Games Adam James True
ANSWER 2
Score 3
Just use pandas.Series.str.contains.
>>> df
title
0 dogs r us
1 pigs can fly
2 dog games
>>> df['Match'] = df.title.str.contains('dog')
>>> df
title Match
0 dogs r us True
1 pigs can fly False
2 dog games True
If you want the check to be case insensitive, you can use a re.IGNORECASE flag.
>>> df['Match'] = df.title.str.contains('dog', flags=re.IGNORECASE)
Since this is using re.search, you can check for multiple strings with a regular regex way, something like
>>> df['Match'] = df.title.str.contains('dog|cats', flags=re.IGNORECASE)