Pat
Pat

Reputation: 1339

Ifelse on pandas data frame based on strings row wise

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
>>>  

Upvotes: 0

Views: 1130

Answers (1)

EdChum
EdChum

Reputation: 394041

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.

Upvotes: 3

Related Questions