【发布时间】:2017-12-09 00:10:58
【问题描述】:
我有一个包含两列医院名称和地址的数据框,我想遍历每个地址以查找纬度和经度。我的代码似乎占据了数据框中的第一行,我似乎无法选择地址来查找坐标。
import pandas
from geopy.geocoders import Nominatim
geolocator = Nominatim()
for index, item in df.iterrows():
location = geolocator.geocode(item)
df["Latitude"].append(location.latitude)
df["Longitude"].append(location.longitude)
这是我用来抓取网站的代码。复制并运行它,您将拥有数据集。
import requests
from bs4 import BeautifulSoup
import pandas
import numpy as np
r=requests.get("https://www.privatehealth.co.uk/hospitals-and-
clinics/orthopaedic-surgery/?offset=300")
c=r.content
soup=BeautifulSoup(c,"html.parser")
all=soup.find_all(["div"],{"class":"col-9"})
names = []
for item in all:
d={}
d["Hospital Name"] = item.find(["h3"],{"class":"mb6"}).text.replace("\n","")
d["Address"] = item.find(["p"],{"class":"mb6"}).text.replace("\n","")
names.append(d)
df=pandas.DataFrame(names)
df = df[['Hospital Name','Address']]
df
目前数据如下(以医院为例):
Hospital Name |Address
Fulwood Hospital|Preston, PR2 9SZ
我试图实现的最终输出如下所示。
Hospital Name |Address | Latitude | Longitude
Fulwood Hospital|Preston, PR2 9SZ|53.7589938|-2.7051618
【问题讨论】:
-
请提供一些示例数据以及您当前和预期的输出。