Type error: cannot convert the series to <class 'float'>
16,443
The problem is quite trivial,
- 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
Below is your updated code:
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'])
is aPandas.Series
type(rep_points['lat'][0])
is aNumpy.Float
- to iterate over a
Pandas.Series
you need to useiteritems
Related videos on Youtube
Author by
AkankshaC
Updated on June 04, 2022Comments
-
AkankshaC almost 2 years
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
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'>
in the line where circle is made.
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)
and
rep_pints['long'].astype(float)
but it didnt work well
-
jezrael about 6 yearsSo
rep_points = rep_points.apply(pd.to_numeric, errors='coerce')
orrep_points = rep_points.apply(pd.to_numeric, errors='coerce').dropna()
for remove rows with missing values (created by converting non numeric values) should working -
iam.Carrot about 6 years@jezrael That's not even the same error. I am not sure that's gonna help
-
jezrael about 6 years@iam.Carrot - OK, so reopened.
-
-
AkankshaC about 6 yearsIf i use the above code i get the error TypeError: zip argument #1 must support iteration in line gmap.plot @iam.Carrot
-
iam.Carrot about 6 yearsI referred to this. can you please share your stack trace
-
AkankshaC about 6 yearslatitude_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
-
iam.Carrot about 6 years@AkankshaC I need the stack trace not the code. When an exception is thrown then what is the error message and what threw the error.
-
iam.Carrot about 6 years@AkankshaC Please use the question to put in your
StackTrace
Use the edit question. Also, Why do you havegmap.marker(rep_points['lat'],rep_points['lon'],title="Cluster")
This code should not be there -
AkankshaC about 6 years--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-188-7a522e6fc5f3> in <module>() 49 50 gmap = gmplot.GoogleMapPlotter(latitude_collection[0], longitude_collection[0], 11) ---> 51 gmap.plot(min(latitude_collection),min(longitude_collection)) 52 gmap.scatter(latitude_collection,longitude_collection,c='aquamarine') 53
-
AkankshaC about 6 years~/anaconda3/lib/python3.6/site-packages/gmplot/gmplot.py in plot(self, lats, lngs, color, c, **kwargs) 118 kwargs.setdefault("color", color) 119 settings = self._process_kwargs(kwargs) --> 120 path = zip(lats, lngs) 121 self.paths.append((path, settings)) 122 TypeError: zip argument #1 must support iteration
-
iam.Carrot about 6 years@AkankshaC can you please update the question with this stack trace? It's tough to figure out things with this formatting. Also, the issue is the
zip
function thelats
is not a collection. Where is thezip
in the code?