Search code examples
pythondataframesubstring

How to check if all multiple strings exist in another string (for all row in dataframe)


How can I check if all multiple strings existing in another string?

Then assign the result to a new column named "value".

Example of the dataframe:

sub_strings string value
["the sun", "rising up"] "the sun is rising up." True
["go home", "tomorow"] "I will go home." False

My code is:

if all(x in df["string"] for x in df["sub_strings"]):
    df['value'] = True
else:
    df['value'] = False

Could you please help me? Thank you in advance.


Solution

  • this code snippet should do your work:

    df = pd.DataFrame({'sub_strings': [["the sun", "rising up"], ["go home", "tomorow"]], 'string': ["the sun is rising up.", "I will go home."]})
    df.head()
    
                sub_strings                 string
    0  [the sun, rising up]  the sun is rising up.
    1    [go home, tomorow]        I will go home.
    
    df['value'] = df.apply(lambda x: all([val in x.string for val in x.sub_strings]), axis=1)
    df.head()
    
                sub_strings                 string  value
    0  [the sun, rising up]  the sun is rising up.   True
    1    [go home, tomorow]        I will go home.  False