【问题标题】:Type error: cannot convert the series to <class 'float'>类型错误:无法将系列转换为 <class 'float'>
【发布时间】:2026-02-21 06:45:01
【问题描述】:
      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

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

Type error: cannot convert the series to <class 'float'> 

在画圈的那一行。

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")

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

rep_pints['lat'].astype(float) 

rep_pints['long'].astype(float) 

但是效果不好

【问题讨论】:

  • 所以rep_points = rep_points.apply(pd.to_numeric, errors='coerce')rep_points = rep_points.apply(pd.to_numeric, errors='coerce').dropna() 用于删除缺失值的行(通过转换非数值创建)应该可以工作
  • @jezrael 这甚至不是同一个错误。我不确定这会有所帮助
  • @iam.Carrot - 好的,所以重新打开了。

标签: python python-3.x pandas google-maps


【解决方案1】:

这个问题很简单,

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

以下是您的更新代码:

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")

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

  1. type(rep_points['lat'])Pandas.Series
  2. type(rep_points['lat'][0])Numpy.Float
  3. 要遍历Pandas.Series,您需要使用iteritems

【讨论】:

  • 如果我使用上面的代码,我会得到错误 TypeError: zip argument #1 must support iteration in line gmap.plot @iam.Carrot
  • 我提到了this。你能分享你的堆栈跟踪吗
  • 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)) gmap.scatter(latitude_collection,longitude_collection,c='aquamarine') #gmap.heatmap(rep_points['lat'],rep_points['long']) gmap.circle (latitude_collection,longitude_collection, 100, color='yellow') gmap.draw("user001_clus_time.html") @iam.Carrot
  • @AkankshaC 我需要堆栈跟踪而不是代码。当抛出异常时,错误消息是什么以及引发错误的原因。
  • @AkankshaC 请使用问题输入您的StackTrace 使用编辑问题。另外,为什么你有gmap.marker(rep_points['lat'],rep_points['lon'],title="Cluster") 这个代码不应该在那里
最近更新 更多