问题描述
我发现了一个非常有趣的帖子,试图适应我的数据。这是帖子的链接。
https://alysivji.github.io/getting-started-with-folium.html
这是我正在测试的代码。
import pandas as pd
import gmplot
import matplotlib.pyplot as plt
import folium
from folium import plugins
import seaborn as sns
df = pd.read_csv('C:\\Users\\ryans\\OneDrive\\Desktop\\business.csv')
m = folium.Map([43.7181557,-79.5181415],zoom_start=11)
m
new_df = df[['longitude','latitude','address']].copy()
new_df.dtypes
# mark each station as a point
for index,row in df.iterrows():
print(row)
folium.CircleMarker([new_df['latitude'],new_df['longitude']],radius=15,popup=new_df['address'],fill_color="#3db7e4",# divvy color
).add_to(m)
# convert to (n,2) nd-array format for heatmap
stationArr = df[['latitude','longitude']].to_numpy()
# plot heatmap
m.add_children(plugins.HeatMap(stationArr,radius=15))
m
我遇到此错误。
ValueError: Location should consist of two numerical values,but 0 33.522143
1 43.605499
2 35.092564
3 33.455613
4 35.190012
9994 43.629300
9995 36.219236
9996 36.035749
9997 36.148016
9998 43.779707
Name: latitude,Length: 9999,dtype: float64 of type <class 'pandas.core.series.Series'> is not convertible to float.
这是我的数据类型。
longitude float64
latitude float64
address object
最后,这是我的new_df的示例。
longitude latitude address
0 -112.018481 33.522143 2818 E Camino Acequia Drive
1 -79.652289 43.605499 30 Eglinton Avenue W
2 -80.859132 35.092564 10110 Johnston Rd,Ste 15
3 -112.395596 33.455613 15655 W Roosevelt St,Ste 237
4 -80.887223 35.190012 4209 Stuart Andrew Blvd,Ste F
... ... ...
9994 -79.625725 43.629300 1135A Crestlawn Drive
9995 -115.278133 36.219236 3240 N Durango Dr
9996 -115.153343 36.035749 7400 Las Vegas Blvd S Ofc
9997 -115.164513 36.148016 2101 Western Ave
9998 -79.418050 43.779707 28 Finch Avenue W,Unit 109
我该如何解决?
解决方法
多玩一点之后,我想到了这个。
X = df[['longitude','latitude','name']].copy()
# mark each station as a point
for index,row in X.iterrows():
folium.CircleMarker([row['latitude'],row['longitude']],radius=15,popup=row['name'],fill_color="#3db7e4",# divvy color
).add_to(m)
# convert to (n,2) nd-array format for heatmap
stationArr = df[['latitude','longitude']].to_numpy()
# plot heatmap
m.add_child(plugins.HeatMap(stationArr,radius=15))
m
这似乎很好。
我从不理解这些不是真实错误的“假错误”。错误:无法转换为浮点数。好吧,它已经是浮点数了,因此机器不应该尝试进行某种转换。