Python Forum

Full Version: ValueError: could not convert string to float
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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?
Please suggest how to resolve it? Should I gather coordinates separately for the locations from my dataset?

Thanks,
Rahul
(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?