title: new column based on row and column conditions pandas python
body: <p>i have a sample data set:</p>
  
  <pre><code>import pandas as pd
  import numpy as np
  
  d = {
  
   'ID': ['A','B','C','D','E'],
   'index_1':[2,0,2,-2,0],
   'index_2':[-2,-2,0,0,0],
   'index_3':[2,2,2,2,0],
   'index_4':[2,2,0,-2,0],
   'index_total':[2,2,2,2,2]
  }
  df = pd.DataFrame(d)
  </code></pre>
  
  <p>it looks like:</p>
  
  <pre><code>   ID   index_1  index_2  index_3   index_4   index_total
  0   A        2       -2        2        2            2
  1   B        0       -2        2        2            2
  2   C        2        0        2        0            2
  3   D       -2        0        2       -2            2
  4   E        0        0        0        0            2
  </code></pre>
  
  <p>i want to create a column named 'flag' based on following conditions <strong>for each row</strong>:</p>
  
  <ol>
  <li>if any of the columns 'index_1', 'index_2', 'index_3', 'index_4' contains the value -2  <strong>AND</strong>  'index_total' = 2  then flag = 1</li>
  <li>if columns 'index_1', 'index_2', 'index_3', 'index_4' only contain the value 0  <strong>AND</strong>  'index_total' = 2  then flag = 1</li>
  <li>else: flag = 0</li>
  </ol>
  
  <p>desired output:</p>
  
  <pre><code>    ID   index_1  index_2  index_3   index_4   index_total   flag
  0    A        2       -2        2        2            2        1
  1    B        0       -2        2        2            2        1
  2    C        2        0        2        0            2        0
  3    D       -2        0        2       -2            2        1
  4    E        0        0        0        0            2        1
  </code></pre>
  
  <p><strong>my attempts (note i use loops for the index_1, index_2, index_3 and index_4 column names instead of writing them out because in my actual data set there are more than 70 index_ columns)</strong></p>
  
  <p><strong>1st attempt:</strong></p>
  
  <pre><code>for colname in df.columns:
      if "index_" in colname:
          df[colname] = df[colname].astype(int)  
   #making sure the numbers are all integer for comparison
      if ((df[colname] == -2).any() and df['index_total']==2):
           df['flag'] = 1
    #this doesn't work , it's going by columns not rows
  </code></pre>
  
  <p><strong>2nd attempt:</strong></p>
  
  <pre><code> for index, row in df.iterrows():    
      for colname in df.columns:
         if "index_" in colname:
             if( (df[colname][index] == -2).any() and df['index_total']==2 ):
                  df['flag'] = 1
   # i stopped writing the other conditions because this one doesn't work
  </code></pre>