无法将系列转换为

无法将系列转换为

本文介绍了类型错误:无法将系列转换为< class'float'>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

      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.当我运行下面的代码时,它给出了错误

The above is a dataframe rep_points. When i run the code below, it gives an error

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

我应如何解决此错误?香港专业教育学院尝试使用

How should i resolve this error? Ive tried using

rep_pints['lat'].astype(float)

rep_pints['long'].astype(float)

但是效果不佳

推荐答案

问题很简单,

  1. 您正在使用Pandas.DataFrame.现在,当您将其切片rep_points['lat']时,您将得到一个Pandas.Series.
  2. gmplot.scatter()期望floatsiterable而不是floatsseries.
  3. 现在,如果您使用rep_points['lat'].tolist()Pandas.Series转换为list,它将开始工作
  1. You're using a Pandas.DataFrame. Now when you slice it rep_points['lat'], you get a Pandas.Series.
  2. The gmplot.scatter() is expecting an iterable of floats not a series of floats.
  3. Now if you convert your Pandas.Series to a list by using rep_points['lat'].tolist() It'll start working

下面是您的更新代码:

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

其他有助于指出这一点的东西:

Other things that helped to point it out:

  1. type(rep_points['lat'])Pandas.Series
  2. type(rep_points['lat'][0])Numpy.Float
  3. 要遍历Pandas.Series,您需要使用iteritems
  1. type(rep_points['lat']) is a Pandas.Series
  2. type(rep_points['lat'][0]) is a Numpy.Float
  3. to iterate over a Pandas.Series you need to use iteritems

这篇关于类型错误:无法将系列转换为&lt; class'float'&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:49