python - 我可以将 for 循环的输出转换为数据框中的列吗?
问题描述
我正在获取巴尔的摩社区的纬度和经度,并且有一个工作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。
解决方案
一种选择是构建矩阵,然后将其转换为 pandas DataFrame。
df = []
for neigh in neigh_list:
try:
geolocator = Nominatim(user_agent="bmore_explorer")
location = geolocator.geocode(neigh)
latitude = location.latitude
longitude = location.longitude
df.append([location, latitude, longitude])
except Exception as e:
print('Error, skipping address...', e)
df = pd.DataFrame(df, columns=['location', 'latitude', 'longitude'])
顺便说一句,pandas 还具有直接从 excel 表中读取的功能,这可以简化您的 readcolumn 功能
推荐阅读
- docker - 在 Ubuntu 16.04 上无法连接到 unix:///var/run/docker.sock 上的 Docker 守护程序
- django - 在 Django admin 中使用 save_model 更改表单字段的“必需”属性
- php - 如何从 Phinx 迁移插入中访问自动生成的 ID?
- laravel - 试图获取非对象 laravel 的属性“id”
- android - 评价视图时,MotionEvent#getX getY 返回异常值
- python - 如何在省略某些数据的同时获取一组 JSON 数据?
- python - Dataflow/apache Beam - 传入模式时如何访问当前文件名?
- unit-testing - SpringBootTest - 请求无效时测试异常
- purescript - PureScript 中的全局资源(适用于 AWS Lambda)
- sql-server - SSRS 2017 带有日期的级联参数