Skip to content Skip to sidebar Skip to footer

Python Pandas Column Conditional On Two Other Column Values

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 colum

Solution 1:

If many columns then simplier is create subset df[['Title', 'Subtitle']] and applycontains, 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          

Solution 2:

pattern= "test|Test"
match= df['Title'].str.contains(pattern) | df['Subtitle'].str.contains(pattern)
df['Test_Flag'] = np.where(match, 'Y', '')

Solution 3:

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

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

enter image description here

Post a Comment for "Python Pandas Column Conditional On Two Other Column Values"