Python Forum
ValueError: could not convert string to float
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: could not convert string to float
#1
Hi,
I am getting -ValueError: could not convert string to float while trying to generate a map via geographical coordinates. My code is below:
I gathered coordinates of various location in a city according to below code.

My data set image is attached for reference.[Image: 3WbuAyXyQY8r7eAH8]

location = [x for x in rest_df['Restaurant_Location'].unique().tolist() if type(x) == str]
latitude = []
longitude =  []
for i in range(0, len(location)):
    if(type(location[i]) == str):
        ctr=0
        while True:
            try:
                address = location[i] + ', Mumbai, India'
                geolocator = Nominatim(user_agent="ny_explorer")
                loc = geolocator.geocode(address)
                latitude.append(loc.latitude)
                longitude.append(loc.longitude)
                print('The geograpical coordinate of location are {}, {}.'.format(loc.latitude, loc.longitude))
            except:
                ctr+=1
                if(ctr==7):
                    print(i)
                    latitude.append(address)
                    longitude.append(address)
                    break
                continue
            break
rest_df['location_latitude'] = rest_df['Restaurant_Location'].map(dict(zip(location, latitude)))
rest_df['location_longitude'] = rest_df['Restaurant_Location'].map(dict(zip(location, longitude)))

dataframe_filtered = rest_df.groupby(['Restaurant_Location'])['location_latitude', 'location_longitude'].first()
dataframe_filtered['no_restaurant'] = rest_df.groupby(['Restaurant_Location'])['Cost_for_two'].count()
venues_map = folium.Map(location=[19.076090, 72.877426], zoom_start=11)

states = folium.map.FeatureGroup()
i=0
for lat, lng, in zip(dataframe_filtered.location_latitude, dataframe_filtered.location_longitude):
    states.add_child(
        folium.CircleMarker(
            [float(lat), float(lng)],
            radius=5, # define how big you want the circle markers to be
            color='yellow',
            fill=True,
            fill_color='blue',
            fill_opacity=0.6,
        )
    )
    i+=1
i=0
for lat, lng, in zip(dataframe_filtered.location_latitude, dataframe_filtered.location_longitude):
    states.add_child(
        folium.Marker(
            [float(lat), float(lng)],
            popup= dataframe_filtered.index[i],
        )
    )
    i+=1
# add incidents to map
venues_map.add_child(states)
venues_map
The error which I am getting is below:

Error:
C:\Users\Klsingh\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be deprecated, use a list instead. --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-57-de12d43743bd> in <module> 8 states.add_child( 9 folium.CircleMarker( ---> 10 [float(lat), float(lng)], 11 radius=5, # define how big you want the circle markers to be 12 color='yellow', ValueError: could not convert string to float: 'Dadar Shivaji Park, Mumbai, India'
Please help. Thank you.

Regards,
Rahul
Reply
#2
The error tells you the problem, doesn't it? The string 'Dadar Shivaji Park, Mumbai, India' doesn't represent a float. Why are you treating that as a latitude or longitude?
Reply
#3
Please suggest how to resolve it? Should I gather coordinates separately for the locations from my dataset?

Thanks,
Rahul
Reply
#4
(Apr-07-2020, 05:49 AM)RahulSingh Wrote: Please suggest how to resolve it? Should I gather coordinates separately for the locations from my dataset?

Thanks,
Rahul

i also had same error have u resolve it?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad ValueError: could not convert string to float badju 0 4,294 Jul-01-2021, 12:13 AM
Last Post: badju
  Indirectlty convert string to float in JSON file WBPYTHON 6 5,830 May-06-2020, 12:09 PM
Last Post: WBPYTHON
  convert a list of string+bytes into a list of strings (python 3) pacscaloupsu 4 10,742 Mar-17-2020, 07:21 AM
Last Post: markfilan
  Convert dataframe string column to numeric in Python darpInd 1 2,270 Mar-14-2020, 10:07 AM
Last Post: ndc85430
  convert 'A B C' to numpy float matrix rezabma 4 2,491 Feb-27-2020, 09:48 AM
Last Post: rezabma
  ValueError: could not convert string to float: '4 AVENUE' Kudzo 4 5,867 Jan-26-2020, 10:47 PM
Last Post: Kudzo
  Convert 'object' to 'string' AdWill97 1 62,346 May-06-2019, 08:22 AM
Last Post: Yoriz
  ValueError: could not convert the string to float Grin 3 10,184 Jun-14-2018, 08:17 PM
Last Post: killerrex
  Problema with convert image to string karlo123 1 2,747 May-16-2018, 10:44 PM
Last Post: karlo123
  Error: ValueError: could not convert string to float: 'L200 1.6 D/C' Jaarroy 2 6,518 Jan-18-2018, 02:00 PM
Last Post: Jaarroy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020