Reputation: 4498
Is there a way in python pandas to apply a conditional if one or another column have a value?
For one column, I know I can use the following code, to apply a test flag if the column Title includes the word "test".
df['Test_Flag'] = np.where(df['Title'].str.contains("test|Test"), 'Y', '')
But if I would like to say if column title or column subtitle include the word "test", add the test flag, how could I do that?
This obviously didn't work
df['Test_Flag'] = np.where(df['Title'|'Subtitle'].str.contains("test|Test"), 'Y', '')
Upvotes: 3
Views: 2188
Reputation: 294506
Using @jezrael's setup
df = pd.DataFrame(
{'Title':['test','Test','e', 'a'],
'Subtitle':['b','a','Test', 'a']})
pandas
you can stack
+ str.contains
+ unstack
import re
df.stack().str.contains('test', flags=re.IGNORECASE).unstack()
Subtitle Title
0 False True
1 False True
2 True False
3 False False
Bring it all together with
truth_map = {True: 'Y', False: ''}
truth_flag = df.stack().str.contains(
'test', flags=re.IGNORECASE).unstack().any(1).map(truth_map)
df.assign(Test_flag=truth_flag)
Subtitle Title Test_flag
0 b test Y
1 a Test Y
2 Test e Y
3 a a
numpy
if performance is a concern
v = df.values.astype(str)
low = np.core.defchararray.lower(v)
flg = np.core.defchararray.find(low, 'test') >= 0
ys = np.where(flg.any(1), 'Y', '')
df.assign(Test_flag=ys)
Subtitle Title Test_flag
0 b test Y
1 a Test Y
2 Test e Y
3 a a
naive time test
Upvotes: 2
Reputation: 863501
If many columns then simplier is create subset df[['Title', 'Subtitle']]
and apply
contains
, because works only with Series
and check at least one True
per row by any
:
mask = df[['Title', 'Subtitle']].apply(lambda x: x.str.contains("test|Test")).any(axis=1)
df['Test_Flag'] = np.where(mask,'Y', '')
Sample:
df = pd.DataFrame({'Title':['test','Test','e', 'a'], 'Subtitle':['b','a','Test', 'a']})
mask = df[['Title', 'Subtitle']].apply(lambda x: x.str.contains("test|Test")).any(axis=1)
df['Test_Flag'] = np.where(mask,'Y', '')
print (df)
Subtitle Title Test_Flag
0 b test Y
1 a Test Y
2 Test e Y
3 a a
Upvotes: 4
Reputation: 6322
pattern = "test|Test"
match = df['Title'].str.contains(pattern) | df['Subtitle'].str.contains(pattern)
df['Test_Flag'] = np.where(match, 'Y', '')
Upvotes: 3