title: Pandas: How to Compare Columns of Lists Row-wise in a DataFrame with Pandas (not for loop)?
body: <p><strong>DataFrame</strong></p>
  
  <pre><code>df = pd.DataFrame({'A': [['gener'], ['gener'], ['system'], ['system'], ['gutter'], ['gutter'], ['gutter'], ['gutter'], ['gutter'], ['gutter'], ['aluminum'], ['aluminum'], ['aluminum'], ['aluminum'], ['aluminum'], ['aluminum'], ['aluminum'], ['aluminum'], ['aluminum'], ['aluminum', 'toledo']], 'B': [['gutter'], ['gutter'], ['gutter', 'system'], ['gutter', 'guard', 'system'], ['ohio', 'gutter'], ['gutter', 'toledo'], ['toledo', 'gutter'], ['gutter'], ['gutter'], ['gutter'], ['how', 'to', 'instal', 'aluminum', 'gutter'], ['aluminum', 'gutter'], ['aluminum', 'gutter', 'color'], ['aluminum', 'gutter'], ['aluminum', 'gutter', 'adrian', 'ohio'], ['aluminum', 'gutter', 'bowl', 'green', 'ohio'], ['aluminum', 'gutter', 'maume', 'ohio'], ['aluminum', 'gutter', 'perrysburg', 'ohio'], ['aluminum', 'gutter', 'tecumseh', 'ohio'], ['aluminum', 'gutter', 'toledo', 'ohio']]}, columns=['A', 'B'])
  </code></pre>
  
  <p><strong>What it Looks Like</strong></p>
  
  <p>I have a dataframe with two columns of lists.</p>
  
  <pre><code>                     A                                      B
  0              [gener]                               [gutter]
  1              [gener]                               [gutter]
  2             [system]                       [gutter, system]
  3             [system]                [gutter, guard, system]
  4             [gutter]                         [ohio, gutter]
  5             [gutter]                       [gutter, toledo]
  6             [gutter]                       [toledo, gutter]
  7             [gutter]                               [gutter]
  8             [gutter]                               [gutter]
  9             [gutter]                               [gutter]
  10          [aluminum]    [how, to, instal, aluminum, gutter]
  11          [aluminum]                     [aluminum, gutter]
  12          [aluminum]              [aluminum, gutter, color]
  13          [aluminum]                     [aluminum, gutter]
  14          [aluminum]       [aluminum, gutter, adrian, ohio]
  15          [aluminum]  [aluminum, gutter, bowl, green, ohio]
  16          [aluminum]        [aluminum, gutter, maume, ohio]
  17          [aluminum]   [aluminum, gutter, perrysburg, ohio]
  18          [aluminum]     [aluminum, gutter, tecumseh, ohio]
  19  [aluminum, toledo]       [aluminum, gutter, toledo, ohio]
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>If I have columns of lists, is there a pandas function that lets me operate on the entire array of lists to check for intersection and return either a boolean or the intersecting values as a new series?</p>
  
  <p>For example, I'd like pandas to have an equivalent of this:</p>
  
  <pre><code>def intersection(df, col1, col2, return_type='boolean'):
      if return_type == 'boolean':
          df = df[[col1, col2]]
          s = []
          for idx in df.iterrows():
              s.append(any([phrase in idx[1][0] for phrase in idx[1][1]]))
          S = pd.Series(s)
          return S
      elif return_type == 'word':
          df = df[[col1, col2]]
          s = []
          for idx in df.iterrows():
              s.append(', '.join([word for word in list(set(idx[1][0]).intersection(set(idx[1][1])))]))
          S = pd.Series(s)
          return S
  
  #Create column C in df
  df['C'] = intersection(df, 'A', 'B', 'word')
  </code></pre>
  
  <p>... without having to write my own function or resort to for loops.  I feel like there must be an easier way to compare lists in two columns on the same row to see if they intersect.</p>
  
  <p><strong>I can do it with <code>for</code> loops but it's ugly to me</strong></p>
  
  <p><code>for</code> loop to return a <code>boolean</code> series:</p>
  
  <pre><code>for idx in df.iterrows():
      any([phrase in idx[1][0] for phrase in idx[1][1]])
  </code></pre>
  
  <p>Produces:</p>
  
  <pre><code>False
  False
  True
  True
  True
  True
  True
  True
  True
  True
  True
  True
  True
  True
  True
  True
  True
  True
  True
  True
  </code></pre>
  
  <p>Or, finding the intersecting words using <code>set</code>s:</p>
  
  <pre><code>for idx in df.iterrows():
      ', '.join([word for word in list(set(idx[1][0]).intersection(set(idx[1][1])))])
  
  ''
  ''
  'system'
  'system'
  'gutter'
  'gutter'
  'gutter'
  'gutter'
  'gutter'
  'gutter'
  'aluminum'
  'aluminum'
  'aluminum'
  'aluminum'
  'aluminum'
  'aluminum'
  'aluminum'
  'aluminum'
  'aluminum'
  'toledo, aluminum'
  </code></pre>