我正在为巴尔的摩的社区获取经度和纬度,并且有一个工作的for
循环,其中列出了具有经度和纬度的社区名称,但需要将输出放入数据框。
# Gets a list from excel spreadsheet
def readcolumn(filename,column):
#select sheet name and selct column as index,index_col=0
df = pd.read_excel(filename)
headername = list(df)
print(headername)
column_data =df[list(df)[column]].tolist()
return column_data
# Converts the neighborhood column in the spreadsheet to a list
neigh_list = readcolumn('BaltimoreNeighborhoods.xlsx', 2)
# Outputs the neighborhood, lat, and long
for neigh in neigh_list:
try:
geolocator = Nominatim(user_agent="bmore_explorer")
location = geolocator.geocode(neigh)
latitude = location.latitude
longitude = location.longitude
print(neigh, latitude, longitude)
except Exception as e:
print('Error, skipping address...', e)
输出看起来像这样:
Arlington, MD 39.3486919 -76.6826661
Ashburton, MD 39.3279621 -76.6710811
Callaway-Garrison, MD 39.3321612 -76.6794359
Central Park Heights, MD 39.3444594 -76.6712351
我想将此列表转换为df。
答案 0 :(得分:1)
一种选择是构建矩阵,然后将其转换为pandas DataFrame。
Optional.ifPresent()
顺便说一句,pandas还具有read straight from an excel sheet的功能,可以简化您的readcolumn函数
答案 1 :(得分:0)
附加行解决了该问题。可能不是最合乎逻辑的方法,但它确实有效。
# Prints out the neighborhood list with the latitude and longitude
df = []
for neigh in neigh_list:
try:
geolocator = Nominatim(user_agent="bmore_explorer")
location = geolocator.geocode(neigh)
latitude = location.latitude
longitude = location.longitude
print(neigh, latitude, longitude)
df.append({'location':location,
'latitude': latitude,
'longitude': longitude})
except Exception as e:
print('Error, skipping address...', e)
df = pd.DataFrame(df, columns=['Location', 'Latitude', ;Longitude'])