首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将列表转换为dataframe有问题-并非所有元素都放入dataframe中

将列表转换为dataframe有问题-并非所有元素都放入dataframe中
EN

Stack Overflow用户
提问于 2021-06-29 21:12:59
回答 1查看 14关注 0票数 0

我一直在尝试使用for循环来获取一些数据,然后将数据放入一个列表中,然后是dataframe。下面是我的代码:

代码语言:javascript
复制
quartiers = ['Centre-ville','Bellevue - Chantenay - Sainte-Anne','Dervallières - Zola','Hauts-Pavés - Saint-Félix','Malakoff - Saint-Donatien','Île de Nantes','Breil - Barberie', 'Nantes Nord', 'Nantes Erdre', 'Doulon - Bottiere', 'Nantes Sud']

quartier_coord = []
for quartier in quartiers:
    address = '{}, Nantes, France'.format(quartier)
    geolocator = Nominatim(user_agent="nantes_explorer_2")
    location = geolocator.geocode(address)
    latitude = location.latitude
    longitude = location.longitude
    print('The geograpical coordinates of {} are {}, {}.'.format(quartier, latitude, longitude))
    quartier_coord = [{'Neighbourhood': quartier, 'Latitude': latitude, 'Longitude': longitude}]

第一部分的输出是OK:

代码语言:javascript
复制
The geograpical coordinates of Centre-ville are 47.214839850000004, -1.557936644670939.
The geograpical coordinates of Bellevue - Chantenay - Sainte-Anne are 47.1977826, -1.5979483854260317.
The geograpical coordinates of Dervallières - Zola are 47.2177889, -1.5889576428457481.
The geograpical coordinates of Hauts-Pavés - Saint-Félix are 47.2287293, -1.5644036887311255.
The geograpical coordinates of Malakoff - Saint-Donatien are 47.223279399999996, -1.536068311104002.
The geograpical coordinates of Île de Nantes are 47.20704805, -1.5462102466548702.
The geograpical coordinates of Breil - Barberie are 47.2351552, -1.5738850630891967.
The geograpical coordinates of Nantes Nord are 47.258410350000005, -1.5663227883277482.
The geograpical coordinates of Nantes Erdre are 47.2649793, -1.5216089444961194.
The geograpical coordinates of Doulon - Bottiere are 47.23940985, -1.5094380518266286.
The geograpical coordinates of Nantes Sud are 47.192114000000004, -1.5324685615599893.

但是列表中只有最后一行的数据:

代码语言:javascript
复制
print(quartier_coord)
['Nantes Sud', 47.192114000000004, -1.5324685615599893]

我不知道我做错了什么。请给我建议。谢谢!

EN

回答 1

Stack Overflow用户

发布于 2021-06-29 21:16:33

您将在每次迭代中重新分配quartier_coord列表;您应该改为追加:

代码语言:javascript
复制
quartier_coord.append([{'Neighbourhood': quartier, 'Latitude': latitude, 'Longitude': longitude}])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68179403

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档