GeoPandas – Transferring Data from Pandas DataFrame to GeoPandas

geopandaspandastypeerror

I am not able to transfer a pandas dataframe column with geolocation coordinates into a Geopandas Point format.

location = pd.read_csv('City_addresses')
print location['Geolocation'].head()

Outputs the following:

0    (50.673675, -120.298973)
1    (50.678354, -120.329258)
2    (50.672496, -120.333317)
3    (50.673359, -120.332912)
4    (50.673411, -120.32978)
Name: Geolocation, dtype: object

Then I attempt to transfer it to Geopandas Point coordinates.

from shapely.geometry import Point
from geopandas import GeoDataFrame

location['Geolocation'] = location['Geolocation'].apply(Point)
gpd = gpd.GeoDataFrame(location, geometry='Geolocation', crs={'init': 'epsg:4326'})

print gpd

To which I get:

TypeError: a float is required

But it won't allow me to change the datatype to a float in pandas either. What should I do?

Best Answer

Your coordinates are string representations of tuples, for example "(50.673675, -120.298973)" but:

The Point constructor takes positional coordinate values or point tuple parameters.

So you need to convert the string to 50.673675 and -120.298973 or (50.673675, -120.298973). You can use eval:

location['geometry'] = location.Geolocation.apply(lambda x: Point(eval(x))