Partial Match If Statement Pandas
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Beneath the City Looping
--
Chapters
00:00 Question
00:50 Accepted answer (Score 4)
01:08 Answer 2 (Score 3)
01:49 Thank you
--
Full question
https://stackoverflow.com/questions/4225...
Answer 1 links:
[pandas.Series.str.contains]: http://pandas.pydata.org/pandas-docs/sta...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas #ifstatement
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Beneath the City Looping
--
Chapters
00:00 Question
00:50 Accepted answer (Score 4)
01:08 Answer 2 (Score 3)
01:49 Thank you
--
Full question
https://stackoverflow.com/questions/4225...
Answer 1 links:
[pandas.Series.str.contains]: http://pandas.pydata.org/pandas-docs/sta...
--
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)