title: Trying to match values in one data frame to values in another data frame (python)
body: <p>I currently have a dataframe A consisting of a column (code1) of country codes such as CA, RU, US etc. I have another dataframe B that has 3 columns where the first column has all possible country codes, the second has a longitude value and the third has a latitude value. I'm trying to loop through the A, get the first country code in the first column, match it to the country code in the first column of B and then get the associated longitude and latitude of that country and so forth. I plan to create a new dataframe containing the codes from A (the first column) and the newly extracted longitude and latitude values.</p>
  <p>So far my function looks as follows</p>
  <pre><code>def get_coords():
      for i in range(len(A[&quot;code1&quot;])):
          for j in range(len(B[&quot;code&quot;])):
              if A[&quot;code1&quot;[i] = B[&quot;code&quot;[j]: #if the country codes match
                  latitude = B[&quot;lat&quot;][j] #gets the latitude of the matched country code
                  longitude = B[&quot;long&quot;][j] #gets the longitude
  </code></pre>
  <p>However, this seems to be inefficient and I'm not sure if it is even matching the codes from the dataframes correctly. Is there a better method of going about what I am trying to achieve?</p>
  <p>For reference <code>len(A[&quot;code1&quot;]) = 581</code> and <code>len(B[&quot;code&quot;] = 5142</code></p>
  <p>Here is a sample input of data:</p>
  <pre><code>A = pd.DataFrame({'code1': ['US', 
                     'RU', 'AO', 'ZW']})
  
  B = pd.DataFrame({'code': ['US', 'ZW', 'RU', 'YE', 'AO'], 
                     'long': [65.216000, 65.216000,18.500000,-63.032000,19.952000], 'lat': [12.500000, 33.677000,-12.500000,18.237000,60.198000]})
  
  </code></pre>
  <p>I am trying to have the output look like</p>
  <pre><code>A = pd.DataFrame({'code1': ['US', 'RU', 'AO', 'ZW'], 'longitude':[65.216000,18.500000, 19.952000, 65.216000], 'latitude': [12.500000, -12.500000, 60.198000, 33.677000]})
  </code></pre>