ValueError: could not convert string to float - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: ValueError: could not convert string to float (/thread-25657.html) |
ValueError: could not convert string to float - RahulSingh - Apr-07-2020 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. 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_mapThe error which I am getting is below: Please help. Thank you.Regards, Rahul RE: ValueError: could not convert string to float - ndc85430 - Apr-07-2020 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?
RE: ValueError: could not convert string to float - RahulSingh - Apr-07-2020 Please suggest how to resolve it? Should I gather coordinates separately for the locations from my dataset? Thanks, Rahul RE: ValueError: could not convert string to float - dinesh - Apr-09-2020 (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? i also had same error have u resolve it? |