title: Remove word in a list from two columns if the word is in both columns in Python
body: <p>Input data:</p>
  <pre><code>data = {'A': ['word1 other stuff', 'otherstuff word1', 'hello word3 bye', 'word1s more stuff'],
          'B': ['foo word1', 'word2 hello', 'word2 bye', 'some str word1']
  }
  
  df = pd.DataFrame (data, columns = ['A', 'B'])
  </code></pre>
  <p>I'm trying to remove words from two strings only if the same word (e.g. word1 <strong>as a standalone word</strong>) is in the same row of column A and column B. In the example provided, word1 should only be removed from the first row and none of the other rows (NOTE! word1 should not be removed from the 4th row since it says &quot;word1s...&quot; and not word1 as a standalone word.)</p>
  <p>OLD CODE:</p>
  <pre><code>## OLD CODE: 
  
  mywordslist = [&quot;word1&quot;, &quot;word2&quot;, &quot;word3&quot;]
  
  for word in mywordslist:
      if ((word in df['A']) and (word in df['B'])):
          df['word_removed'] = 1 ## indicator if both A and B contained the word.
          df['A_new'] = df['A'].apply(lambda x: re.sub(r'\b{}\b'.format(re.escape(word)), ' ', x)
          df['A_new'] = df['A_new'].apply(lambda x: re.sub(r'\b{}$'.format(re.escape(word)), ' ', x)
          df['A_new'] = df['A_new'].apply(lambda x: re.sub(r'${}\b'.format(re.escape(word)), ' ', x)
      else: 
          print('word not in both A and B')
  
  </code></pre>
  <p>So in the example, word1 should be removed from the first row since word1 is in the first row of both A and B.</p>
  <p>The code runs but even though there are many instances where the words should be removed, they are not and the indicator does not show that the word is in both strings. How should I correctly specify the if statement?</p>
  <p>Using the code suggestion provided by @IoaTzimas, word1 gets removed also from examples like in row 4 where word1 is in A but word1s is in B so both A and B should be kept as they are without removing word1:</p>
  <pre><code>### Adopting @IoaTzimas suggestion:
  
  def fA(x,y):
      for k in mywordslist:
         if k in x and k in y:
             x=re.sub(r'\b{}\b'.format(re.escape(k)), ' ', x)
      return x
  
  def fB(x,y):
      for k in mywordslist:
         if k in x and k in y:
             y=re.sub(r'\b{}\b'.format(re.escape(k)), ' ', y)
      return y
  
  df['newA'] = df.apply(lambda x: fA(x.A, x.B), axis=1)
  df['newB'] = df.apply(lambda x: fB(x.A, x.B), axis=1)
  </code></pre>
  <p>NEW QUESTION: How can I remove word1 only in row1 where word1 is standalone? (i.e. I want to only remove r'\bk\b' and not any occurrence of k in combination with other characters).</p>
  <p>EDIT: This seems to solve the issue:</p>
  <pre><code>mywordslist = [&quot;word1&quot;, &quot;word2&quot;, &quot;word3&quot;] 
  
  def fA(x,y):
      for k in mywordslist:
          if re.search(r'\b' + re.escape(k) + r'\b', x) and re.search(r'\b' + re.escape(k) + r'\b', y):
              x=x.replace(k,' ')
              ## x=re.sub(r'\b{}$'.format(re.escape(k)), ' ', x) If only want to remove the word from the end of the string.
      return x
  
  def fB(x,y):
      for k in mywordslist:
          if re.search(r'\b' + re.escape(k) + r'\b', x) and re.search(r'\b' + re.escape(k) + r'\b', y):
              y=y.replace(k, ' ')
      return y
  
  df['newA'] = df.apply(lambda x: fA(x.A, x.B), axis=1)
  df['newB'] = df.apply(lambda x: fB(x.A, x.B), axis=1)
  </code></pre>