Вот кусок кода, который должен работать, я удалил некоторые вещи, так как я не уверен, почему, но вы все равно можете вернуть их обратно, если вы делаете другие вещи в нем. new_df
должен иметь все строки из вашего исходного кадра данных и добавлять два столбца, которые вы хотите. Я не смог проверить, так как у меня нет ваших данных, поэтому они могут опечататься, но идея есть
location = [x for x in rest_df['Restaurant_Location'].unique().tolist()
if type(x) == str]
latitude = []
longitude = []
for i in range(0, len(location)):
# remove things that does not seem usefull here
try:
address = location[i] + ', Mumbai, India'
geolocator = Nominatim(user_agent="ny_explorer")
loc = geolocator.geocode(address)
latitude.append(loc.latitude)
longitude.append(loc.longitude)
print('The geographical coordinate of location are {}, {}.'.format(loc.latitude, loc.longitude))
except:
# in the case the geolocator does not work, then add nan element to list
# to keep the right size
latitude.append(np.nan)
longitude.append(np.nan)
# create a dataframe with the locatio, latitude and longitude
df_ = pd.DataFrame({'Restaurant_Location':location,
'location_latitude': latitude,
'location_longitude':longitude})
# merge on Restaurant_Location with rest_df to get the column
new_df = rest_df.merge(df_, on='Restaurant_Location', how='left')