3 回答
TA贡献1946条经验 获得超4个赞
您可以通过以下方式添加带有地址的列:
import pandas as pd
data = [['madurai',10],['NaN',12],['hosur',13]]
df = pd.DataFrame(data,columns=['Name','Age'])
from geopy.geocoders import Nominatim
geolocator = Nominatim()
for i in df.Name:
if i == "NaN":
continue
df.loc[df.Name == i, 'Address'] = geolocator.geocode(i)
print(df)
TA贡献1790条经验 获得超9个赞
您只能获得最后一个值,因为您loc每次都在循环中进行替换。该GeocoderTimedOut: Service timed out错误的出现是因为你正在为许多请求到服务器。您应该sleep在请求之间包含一个。
尝试:
import pandas as pd
from geopy.geocoders import Nominatim
import time
data = [['madurai',10],['NaN',12],['hosur',13]]
df = pd.DataFrame(data,columns=['Name','Age'])
geolocator = Nominatim(user_agent='test')
address = []
for i in df.Name:
time.sleep(3)
if i == "NaN":
address.append('NaN')
continue
address.append(geolocator.geocode(i))
df['address'] = address
TA贡献1816条经验 获得超6个赞
我在下面的请求之间引入了时间延迟和几行来查看进度条
from geopy.geocoders import Nominatim
geolocator = Nominatim()
from geopy.extra.rate_limiter import RateLimiter
geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)
final['Geolocation'] = final['city'].apply(geocode)
from tqdm import tqdm
tqdm.pandas()
final['Geolocation'] = final['city'].progress_apply(geocode)
它现在有效。
添加回答
举报