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.
So far my function looks as follows
def get_coords():
for i in range(len(A["code1"])):
for j in range(len(B["code"])):
if A["code1"[i] = B["code"[j]: #if the country codes match
latitude = B["lat"][j] #gets the latitude of the matched country code
longitude = B["long"][j] #gets the longitude
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?
For reference len(A["code1"]) = 581
and len(B["code"] = 5142
Here is a sample input of data:
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]})
I am trying to have the output look like
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]})