类型错误:无法将系列转换为 <class 'float'>

Aka*_*haC 2 python google-maps python-3.x pandas

      lat        long       time
 0  39.991861  116.344372   2.823611
 1  39.979768  116.310597  22.263056
 2  31.235001  121.470624  13.141667
 3  31.248822  121.460637   1.805278
Run Code Online (Sandbox Code Playgroud)

上面是一个数据帧 rep_points。当我运行下面的代码时,它给出了一个错误

Type error: cannot convert the series to <class 'float'> 
Run Code Online (Sandbox Code Playgroud)

在圆圈所在的那一行。

gmap = gmplot.GoogleMapPlotter(rep_points['lat'][0], rep_points['long'][0], 11)
gmap.plot(df_min.lat, df_min.lng)
gmap.scatter(rep_points['lat'],rep_points['long'],c='aquamarine')
gmap.circle(rep_points['lat'],rep_points['long'], 100, color='yellow')  
gmap.draw("user001_clus_time.html")
Run Code Online (Sandbox Code Playgroud)

我应该如何解决这个错误?我试过使用

rep_pints['lat'].astype(float) 
Run Code Online (Sandbox Code Playgroud)

rep_pints['long'].astype(float) 
Run Code Online (Sandbox Code Playgroud)

但效果不佳

iam*_*rot 5

问题很简单,

  1. 你正在使用一个Pandas.DataFrame. 现在当你切片它时rep_points['lat'],你会得到一个Pandas.Series.
  2. gmplot.scatter()期待一个iterablefloats不是seriesfloats
  3. 现在,如果您通过使用将您的转换Pandas.Series为 a它将开始工作listrep_points['lat'].tolist()

以下是您更新的代码:

rep_points = pd.read_csv(r'C:\Users\carrot\Desktop\ss.csv', dtype=float)
latitude_collection = rep_points['lat'].tolist()
longitude_collection = rep_points['long'].tolist()

gmap = gmplot.GoogleMapPlotter(latitude_collection[0], longitude_collection[0], 11)
gmap.plot(min(latitude_collection), min(longitude_collection).lng)
gmap.scatter(latitude_collection,longitude_collection,c='aquamarine')
gmap.circle(latitude_collection,longitude_collection, 100, color='yellow')  
gmap.draw("user001_clus_time.html")
Run Code Online (Sandbox Code Playgroud)

其他有助于指出这一点的事情:

  1. type(rep_points['lat']) 是一个 Pandas.Series
  2. type(rep_points['lat'][0]) 是一个 Numpy.Float
  3. 迭代Pandas.Series你需要使用的iteritems