title: Pandas DataFrame - Creating a new column from a comparison
body: <p>I'm trying to create a columns called 'city_code' with values from the 'code' column. But in order to do this I need to compare if 'ds_city' and 'city' values are equal.</p>
  
  <p>Here is a table sample:</p>
  
  <p><a href="https://i.imgur.com/093GJF1.png" rel="nofollow noreferrer">https://i.imgur.com/093GJF1.png</a></p>
  
  <p>I've tried this:</p>
  
  <pre><code>def find_code(data):
      if data['ds_city'] == data['city'] :
          return data['code']
      else:
          return 'UNKNOWN'
  
  df['code_city'] = df.apply(find_code, axis=1)
  </code></pre>
  
  <p>But since there are duplicates in the 'ds_city' columns that's the result:</p>
  
  <p><a href="https://i.imgur.com/geHyVUA.png" rel="nofollow noreferrer">https://i.imgur.com/geHyVUA.png</a></p>
  
  <p>Here is a image of the expected result:</p>
  
  <p><a href="https://i.imgur.com/HqxMJ5z.png" rel="nofollow noreferrer">https://i.imgur.com/HqxMJ5z.png</a></p>
  
  <p>How can I work around this?</p>