title: Optimizing repeated query (mask) of large pandas dataframe
body: <p>I have a pandas dataframe which has around 350 columns and 500000 rows initially:</p>
  
  <pre><code>import string
  import numpy as np
  import pandas as pd
  import itertools
  
  cols = list(string.ascii_lowercase) + [i+j for i,j in [*itertools.combinations(list(string.ascii_lowercase), 2)]]
  df = pd.DataFrame({col: np.repeat([np.random.randint(2)], [500000]) for col in cols})
  </code></pre>
  
  <p>I need to add 3000 new columns to my dataframe (initialized to 0) where the value of each row depends on the values in the existing rows (I use a mask when testing for this): </p>
  
  <pre><code>for i, j, k in itertools.combinations(list(string.ascii_lowercase), 3):
      df[i+j+k] = 0
      df.loc[(df[i] &gt; 0) &amp; (df[j] &gt; 0) &amp; (df[k] &gt; 0) &amp; (df[i + j] + df[i + k] + df[j + k] &gt;= 2), i+j+k] = 1
  </code></pre>
  
  <p>However, the issue is that the above loop is extremely slow! Is there a way to optimize the procedure above? Perhaps using a pandas lookup-function that is faster? </p>