Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

str.contains pandas returns 'str' object has no attribute 'contains'

Tags:

python

pandas

I am analyzing tweets.

I have 10k tweets and am interested in a list of words occurring:

lst1=['spot','mistake']
lst1_tweets=tweets[tweets['tweet_text'].str.contains('|'.join(lst1))].reset_index()

I want to double check and have:

f=lst1_tweets['tweet_text'][0]
f='Spot the spelling mistake Welsh and Walsh. You are showing picture of presenter Bradley Walsh who is alive and kick'
type(f)
<class 'str'>

I used

f.str.contains('|'.join(lst1))

returns:

AttributeError: 'str' object has no attribute 'str'

also

f.contains('|'.join(lst1))

returns:

AttributeError: 'str' object has no attribute 'contains'

Any suggestions how I can search for a list of words in a string

like image 576
frank Avatar asked Nov 08 '19 21:11

frank


2 Answers

I think you are looking for in:

if 'goat' in 'goat cheese':
    print('beeeeeeh!')
like image 149
jorijnsmit Avatar answered Oct 17 '22 12:10

jorijnsmit


You might be confusing .str.contains() from pandas, which exists and is applied to series. In this case you can use in or not in operators. Here's a full guide on how to address the issue Does Python have a string 'contains' substring method?

From pandas docs:

Series.str.contains(self, pat, case=True, flags=0, na=nan, regex=True). Test if pattern or regex is contained within a string of a Series or Index.

like image 11
Celius Stingher Avatar answered Oct 17 '22 13:10

Celius Stingher