循环并存储坐标

问题描述 投票:0回答:1

我有一个数据帧的副本,看起来像这样:

heatmap_df = test['coords'].copy()
heatmap_df


0     [(Manhattanville, Manhattan, Manhattan Communi...
1     [(Mainz, Rheinland-Pfalz, 55116, Deutschland, ...
2     [(Ithaca, Ithaca Town, Tompkins County, New Yo...
3     [(Starr Hill, Charlottesville, Virginia, 22903...
4     [(Neuchâtel, District de Neuchâtel, Neuchâtel,...
5     [(Newark, Licking County, Ohio, 43055, United ...
6     [(Mae, Cass County, Minnesota, United States o...
7     [(Columbus, Franklin County, Ohio, 43210, Unit...
8     [(Canaanville, Athens County, Ohio, 45701, Uni...
9     [(Arizona, United States of America, (34.39534...
10    [(Enschede, Overijssel, Nederland, (52.2233632...
11    [(Gent, Oost-Vlaanderen, Vlaanderen, België - ...
12    [(Reno, Washoe County, Nevada, 89557, United S...
13    [(Grenoble, Isère, Auvergne-Rhône-Alpes, Franc...
14    [(Columbus, Franklin County, Ohio, 43210, Unit...

每行的格式都带有一些坐标:

heatmap_df[2]
[Location(Ithaca, Ithaca Town, Tompkins County, New York, 14853, United States of America, (42.44770298533052, -76.48085858627931, 0.0)),
 Location(Chapel Hill, Orange County, North Carolina, 27515, United States of America, (35.916920469999994, -79.05664845999999, 0.0))]

我想从每一行中提取经度和纬度,并将它们存储为数据帧heatmap_df中的单独列。到目前为止,我已经掌握了这一点,但是我很讨厌编写循环。我的循环无法递归工作,它只打印出最后的坐标。

x = np.arange(start=0, stop=3, step=1)
for i in x:
    point_i = (heatmap_df[i][0].latitude, heatmap_df[i][0].longitude)
    i = i+1
point_i

(42.44770298533052, -76.48085858627931)

我正在尝试使用Folium绘制包含所有坐标的热图。有人可以帮忙吗?谢谢

python loops coordinates heatmap folium
1个回答
0
投票

Python不知道您要做什么,它假设您想为每次迭代将(heatmap_df[i][0].latitude, heatmap_df[i][0].longitude)的元组值存储在变量point_i中。因此,发生的情况是每次都会覆盖它。您想在外部声明一个列表,然后在append中将纬度和经度的列表循环到其上,以创建可以轻松成为DF的列表列表。另外,示例中的循环不是递归的,请检查this的递归

尝试一下:

x = np.arange(start=0, stop=3, step=1)
points = []
for i in x:
    points.append([heatmap_df[i][0].latitude, heatmap_df[i][0].longitude])
    i = i+1
print(points)
© www.soinside.com 2019 - 2024. All rights reserved.