title: Python Pandas - Lookup a variable column depending on another column's value
body: <p>I'm trying to use the value of one cell to find the value of a cell in another column. The first cell value ('source') dictates which column to lookup.</p>
  <pre><code>import pandas as pd
  
  df = pd.DataFrame({'A': ['John', 'Andrew', 'Bob', 'Fred'], 'B': [
                    'Fred', 'Simon', 'Andrew', 'Andrew'], 'source': ['A', 'B', 'A', 'B']}, )
  
  print(df)
  
          A       B source
  0    John    Fred      A
  1  Andrew   Simon      B
  2     Bob  Andrew      A
  3    Fred  Andrew      B
  </code></pre>
  <p>My required output value in the 'output' column is a lookup of the 'source':</p>
  <pre><code>        A       B source  output
  0    John    Fred      A    John
  1  Andrew   Simon      B   Simon
  2     Bob  Andrew      A     Bob
  3    Fred  Andrew      B  Andrew
  </code></pre>
  <p><strong>Failed attempts</strong></p>
  <pre><code>df['output'] = df[df['source']]
  </code></pre>
  <p>This results in a <em>ValueError: Wrong number of items passed 4, placement implies 1</em> because the <code>df['source']</code> passes in a Series, not a string. I tried converting to a string using:</p>
  <pre><code>df['output'] = df[df['source'].convertDTypes(convert_string=True)]
  </code></pre>
  <p>which gave error <em>AttributeError: 'Series' object has no attribute 'convertDTypes'</em>.</p>
  <p><strong>Working solution</strong></p>
  <p>I found a solution might by iterating through the rows using:</p>
  <pre><code>for index, row in df.iterrows():
      column = df.loc[index, 'source']
      df.at[index, 'output'] = df.loc[index, column]
  </code></pre>
  <p>However, <a href="https://stackoverflow.com/a/55557758/15440604">this post</a> suggests iterating is a bad idea. The code doesn't seem very elegant, either.</p>
  <p>I feel I've missed something basic here; this really should not be that hard.</p>