0

This is an easy one. The task is to check if a string in one column contains all words stored in another string. Based on this do something. Here is a simple example

import pandas as pd

df = pd.DataFrame({'Strings':["The brown","fox smoked 6", "cigarettes per day", "in his cave"], 
'Set': ["Alpha", "Beta", "Gamma", "Delta"]})

... >>> df
     Set             Strings
0  Alpha           The brown
1   Beta        fox smoked 6
2  Gamma  cigarettes per day
3  Delta         in his cave
>>> 

Now i want to check in each row of df["Strings"] if it contains the word "smoked" and the number "6" (which is true for row 3 here). If so, I need the new column df["Result"] to be equal to df["Set"] but with the words "health damaging" added to it. If not just copy what's contained in df["Set"]. Output should look like this:

... >>> df_final
     Set             Strings   Result
0  Alpha           The brown   Alpha
1   Beta        fox smoked 6   Beta health damaging
2  Gamma  cigarettes per day   Gamma
3  Delta         in his cave   Delta
>>>  

1 Answer 1

3

You can construct a mask of your 2 conditions and pass this to np.where:

In [20]:

mask = (df['Strings'].str.contains('6')) & (df['Strings'].str.contains('smoked'))
In [23]:

et
df['Result'] = np.where(mask, df['Set'] + ' health damaging', df['Set'])
df
Out[23]:
     Set             Strings                Result
0  Alpha           The brown                 Alpha
1   Beta        fox smoked 6  Beta health damaging
2  Gamma  cigarettes per day                 Gamma
3  Delta         in his cave                 Delta

Here the mask tests for the presence of your strings using .str.contains and we and the conditions together to make the mask.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! that was fast. I will wait one day before praising your answer - maybe someone comes up with another solution but this one is very nice. Thanks again.

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.