我有一个csv,其中包含许多不同的Lat Longs,格式准确,如下所示:39.360611,-74.431877
运行以下代码:
import csv
from folium import plugins
heatmap_map = folium.Map(location=[48, -102], zoom_start=3)
with open('geolocation.csv', "r") as f:
reader = csv.reader(f)
data = [[row[0], row[1]] for row in reader]
print(data)
hm = plugins.HeatMap(data)
heatmap_map.add_children(hm)
f.close()
heatmap_map.save("heatmap.html") 给出以下输出
[['39.360611', '-74.431877']]
Traceback (most recent call last):
File "C:\Users\dge\Anaconda3\lib\site-packages\folium\utilities.py", line 59, in validate_location
float(coord)
ValueError: could not convert string to float: '-'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".\heat.py", line 12, in <module>
hm = plugins.HeatMap(data)
File "C:\Users\dge\Anaconda3\lib\site-packages\folium\plugins\heat_map.py", line 67, in __init__
for line in data]
File "C:\Users\dge\Anaconda3\lib\site-packages\folium\plugins\heat_map.py", line 67, in <listcomp>
for line in data]
File "C:\Users\dge\Anaconda3\lib\site-packages\folium\utilities.py", line 63, in validate_location
.format(coord, type(coord)))
ValueError: Location should consist of two numerical values, but '-' of type <class 'str'> is not convertible to float.似乎python正在将符号识别为字符串,而不是一部分负数。
发布于 2020-03-02 19:29:49
在尝试各种方法时,我发现Pandas是从CSV获取数据到热图的最佳方法。“守则”如下:
import folium
from folium import plugins
import pandas as pd
df = pd.DataFrame()
heatmap_map = folium.Map(location=[48, -102], zoom_start=3)
with open(r'geolocation.csv', "r") as f:
df = df.append(pd.read_csv(f),ignore_index = True)
df = df.dropna()
hm = plugins.HeatMap(df)
heatmap_map.add_child(hm)
f.close()
heatmap_map.save("heatmap.html")请注意,由于某些原因,dropna始终在末尾输入一个空行,这解决了将此类信息传递给热图时可能遇到的问题。
https://stackoverflow.com/questions/60491819
复制相似问题