1

I have a list of strings that have spaces in them:

lst = ["apple pie", "chocolate shakes", "orange juice"]

and I have a series:

       Explanation 

a      "apple pie is my favorite dessert" 
b      "I love chocolate. But I love chocolate shakes more." 
c      "she is allergic to orange juice" 

and I'm looking to get something like this:

        Explanation                                         Explanation Extracted

a      "apple pie is my favorite dessert"                           "apple pie"
b      "I love chocolate. But I love chocolate shakes more."      "chocolate shakes"
c      "she is allergic to orange juice"                          "orange juice"
1
  • What have you tried, and what went wrong with your attempts? For example, there is a handy built-in Series.str.extract() function that seems like exactly what you need Commented Jun 24, 2022 at 21:21

1 Answer 1

2

I will recommend you use str.findall, since you may have mutiple match within one sentence

df['Explanation Extracted'] = df['Explanation'].str.findall('|'.join(lst))
df
Out[193]: 
                                         Explanation Explanation Extracted
0                  apple pie is my favorite dessert            [apple pie]
1  I love chocolate. But I love chocolate shakes ...    [chocolate shakes]
2                   she is allergic to orange juice         [orange juice]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.