本文介绍了类型错误:无法将系列转换为< 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)
但是效果不佳
推荐答案
问题很简单,
- 您正在使用
Pandas.DataFrame
.现在,当您将其切片rep_points['lat']
时,您将得到一个Pandas.Series
. -
gmplot.scatter()
期望floats
的iterable
而不是floats
的series
. - 现在,如果您使用
rep_points['lat'].tolist()
将Pandas.Series
转换为list
,它将开始工作
- You're using a
Pandas.DataFrame
. Now when you slice itrep_points['lat']
, you get aPandas.Series
. - The
gmplot.scatter()
is expecting aniterable
offloats
not aseries
offloats
. - Now if you convert your
Pandas.Series
to alist
by usingrep_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:
-
type(rep_points['lat'])
是Pandas.Series
-
type(rep_points['lat'][0])
是Numpy.Float
- 要遍历
Pandas.Series
,您需要使用iteritems
type(rep_points['lat'])
is aPandas.Series
type(rep_points['lat'][0])
is aNumpy.Float
- to iterate over a
Pandas.Series
you need to useiteritems
这篇关于类型错误:无法将系列转换为< class'float'>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!