0

I am trying to search for words in string account lock in text encoded as Pandas Series. Currently, I am using contains function.

If text is account lock email I am getting correct results.

>>> pd.Series('account lock email').str.contains('account lock', case=False)
0    True
dtype: bool

But if text is lock email account I am getting result False.

>>> pd.Series('lock email account').str.contains('account lock', case=False)
0    False
dtype: bool

Also if text is account email lock I am getting result False.

>>> pd.Series('account email lock').str.contains('account lock', case=False)
0    False
dtype: bool

Is there any way that contains function can be used to check if all words in a specified string are present in Pandas Series?

Or is there any alternative?

Got required results using below code using all function

>>> all(x in ('lock email account').split() for x in ('account lock').split())
True
>>> all(x in ('account email lock').split() for x in ('account lock').split())
True
>>> all(x in ('account email').split() for x in ('account lock').split())
False
user3734568
  • 1,311
  • 2
  • 22
  • 36
  • Try to use search for every word individually – mooga Feb 21 '19 at 09:03
  • You have several pandas solutions [here](https://stackoverflow.com/questions/37011734). – Wiktor Stribiżew Feb 21 '19 at 09:05
  • 1
    Possible duplicate of [Check if two words are in a string](https://stackoverflow.com/questions/40088559/python-check-if-two-words-are-in-a-string) They suggest using the `all` function. – FChm Feb 21 '19 at 09:05
  • You can use regex. Here's how you can `pd.Series('lock email account').str.contains('account|lock', case=False)` – Mayur Bhangale Feb 21 '19 at 09:06
  • @FChm, Thanks for your suggestion , I able to get require results using all function, I have edit my question with all command – user3734568 Feb 21 '19 at 14:14
  • @MayurBhangale, Thanks for your suggestion, your suggestion also worked but I was also looking for code which will be looking for getting True results when both words will be available in text , If i use | as below getting True as result (>>> pd.Series('lock account').str.contains('account|lock', case=False) 0 True dtype: bool) – user3734568 Feb 21 '19 at 14:17

0 Answers0