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
Solution 1:
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.
Post a Comment for "Ifelse On Pandas Data Frame Based On Strings Row Wise"