Reputation: 539
I've been been trying the lat and long of dataframe using geocoder.
I managed to get the lat/long of distict city and save it into a file. However, I got an error when try to map the data to other data of city as Im not sure how to do so.
My code:
def func(a):
if a in data2['CITY']:
return data2['LATITUDE']
else:
return "0"
data["LATITUDE"] = data["CITY"].apply(lambda x: func(x))
Upvotes: 1
Views: 197
Reputation: 41327
If I understand correctly, you can generate data['LATITUDE']
by reindexing.
Set data2
's index to CITY
and reindex()
against data
's CITY
:
data['LATITUDE'] = data2.set_index('CITY').reindex(data['CITY'])['LATITUDE'].values
Upvotes: 1
Reputation: 154
In pandas
, if you check if a certain element is in a pd.Series
, you need to first get it to an array form:
'SCOTLAND' in data2['CITY'] -> False
'SCOTLAND' in data2['CITY'].values -> True
After making this fix, you need to make it so that it returns a single element, not a vector. So, finally:
def func(a):
if a in data2['CITY'].values:
return data2.loc[data2['CITY'] == a, 'LATITUDE'].item()
else:
return "0"
Upvotes: 1