title: Conditional generation of new column - Pandas
body: <p>I am trying to create a new column based on conditional logic on pre-existing columns. I understand there may be more efficient ways to achieve this but I have a few conditions that need to be included. This is just the first step.</p>
  
  <p>The overall scope is to create two new columns that are mapped from <code>1</code> and <code>2</code>. These are referenced to the <code>Object</code> column as I can have multiple rows for each time point. </p>
  
  <p><code>Object2</code> and <code>Value</code> determine how to map the new columns. So if <code>Value is == X</code>, I want to match both <code>Object</code> columns to return the corresponding <code>1</code> and <code>2</code> for that time point to a new column. The same process should occur if <code>Value is == Y</code>. If <code>Value is == Z</code>, I want to insert <code>0, 0</code>. Everything else should be <code>NaN</code></p>
  
  <pre><code>df = pd.DataFrame({   
          'Time' : ['2019-08-02 09:50:10.1','2019-08-02 09:50:10.1','2019-08-02 09:50:10.2','2019-08-02 09:50:10.3','2019-08-02 09:50:10.3','2019-08-02 09:50:10.4','2019-08-02 09:50:10.5','2019-08-02 09:50:10.6','2019-08-02 09:50:10.6'],
          'Object' : ['B','A','A','A','C','C','C','B','B'],
          '1' : [1,3,5,7,9,11,13,15,17],  
          '2' : [0,1,4,6,8,10,12,14,16],     
          'Object2' : ['A','A',np.nan,'C','C','C','C','B','A'],                 
          'Value' : ['X','X',np.nan,'Y','Y','Y','Y','Z',np.nan],                  
          })
  
  def map_12(df):
  
  for i in df['Value']:
      if i == 'X':
          df['A1'] = df['1']
          df['A2'] = df['2']
      elif i == 'Y':
          df['A1'] = df['1']
          df['A2'] = df['2']     
      elif i == 'Z':
          df['A1'] = 0
          df['A2'] = 0             
      else:
          df['A1'] = np.nan
          df['A2'] = np.nan              
  
  return df
  </code></pre>
  
  <p>Intended Output:</p>
  
  <pre><code>                    Time Object   1   2 Object2 Value    A1    A2
  0  2019-08-02 09:50:10.1      A   1   0       A     X   1.0   0.0 # Match A-A at this time point, so output is 1,0
  1  2019-08-02 09:50:10.1      B   3   1       A     X   1.0   0.0 # Still at same time point so use 1,0 
  2  2019-08-02 09:50:10.2      A   5   4     NaN   NaN   NaN   NaN # No Value so NaN
  3  2019-08-02 09:50:10.3      C   7   6       C     Y   7.0   6.0 # Match C-C at this time point, so output is 7,6
  4  2019-08-02 09:50:10.3      A   9   8       C     Y   7.0   6.0 # Still at same time point so use 7,6 
  5  2019-08-02 09:50:10.4      C  11  10       C     Y  11.0  10.0 # Match C-C at this time point, so output is 11,10
  6  2019-08-02 09:50:10.5      C  13  12       C     Y  13.0  12.0 # Match C-C at this time point, so output is 13,12
  7  2019-08-02 09:50:10.6      B  15  14       B     Z   0.0   0.0 # Z so 0,0
  8  2019-08-02 09:50:10.6      B  17  16       A   NaN   NaN   NaN # No Value so NaN
  </code></pre>
  
  <p>New sample df:</p>
  
  <pre><code> df = pd.DataFrame({   
          'Time' : ['2019-08-02 09:50:10.1','2019-08-02 09:50:10.1','2019-08-02 09:50:10.2','2019-08-02 09:50:10.3','2019-08-02 09:50:10.3','2019-08-02 09:50:10.4','2019-08-02 09:50:10.5','2019-08-02 09:50:10.6','2019-08-02 09:50:10.6'],
          'Object' : ['B','A','A','A','C','C','C','B','B'],
          '1' : [1,3,5,7,9,11,13,15,17],  
          '2' : [0,1,4,6,8,10,12,14,16],     
          'Object2' : ['A','A',np.nan,'C','C','C','C','B','A'],                 
          'Value' : ['X','X',np.nan,'Y','Y','Y','Y','Z',np.nan],                
          })
  </code></pre>
  
  <p>Intended Output:</p>
  
  <pre><code>                    Time Object   1   2 Object2 Value    A1    A2
  0  2019-08-02 09:50:10.1      B   1   0       A     X   3.0   1.0 # Match A-A at this time point, so output is 3,1
  1  2019-08-02 09:50:10.1      A   3   1       A     X   3.0   1.0 # Still at same time point so use 3,1 
  2  2019-08-02 09:50:10.2      A   5   4     NaN   NaN   NaN   NaN # No Value so NaN
  3  2019-08-02 09:50:10.3      A   7   6       C     Y   9.0   8.0 # Match C-C at this time point, so output is 9,8
  4  2019-08-02 09:50:10.3      C   9   8       C     Y   9.0   8.0 # Still at same time point so use 9,8 
  5  2019-08-02 09:50:10.4      C  11  10       C     Y  11.0  10.0 # Match C-C at this time point, so output is 11,10
  6  2019-08-02 09:50:10.5      C  13  12       C     Y  13.0  12.0 # Match C-C at this time point, so output is 13,12
  7  2019-08-02 09:50:10.6      B  15  14       B     Z   0.0   0.0 # Z so 0,0
  8  2019-08-02 09:50:10.6      B  17  16       A   NaN   NaN   NaN # No Value so NaN
  </code></pre>