3

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?

2 Answers 2

4

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))
3

Your coordinates are stored inside tuples. However, to construct a GeoPandas Object, you need a column usually named "geometry" which store shapely.geometry objects. In your case, the object needs to be Point objects. Therefore, the code to construct a GeoPandasDataFrame would look like:

from shapely.geometry import Point

location = pd.read_csv('City_addresses')
location["geometry"] = location.apply(lambda x: Point(x["Geolocation"][0], x["Geolocation"][1]) , axis = 1)
gdf = gpd.GeoDataFrame(location, geometry='geometry', crs={'init': 'epsg:4326'})
6
  • You can also do: location['geometry'] = location.Geolocation.apply(lambda x: Point(eval(x)))
    – Bera
    Commented Jul 17, 2018 at 8:07
  • @yimcai Your solution is giving me a type error, says that the float type is required Commented Aug 5, 2018 at 5:32
  • @BERA good to know.
    – yimcai
    Commented Aug 5, 2018 at 9:20
  • @user4718221 sorry I did not notice that (50.673675, -120.298973) is actually stored as String type "(50.673675, -120.298973)". My method requires some extra string parsing work. Instead, BERA provides a shortcut to save work to parse "(50.673675, -120.298973)"
    – yimcai
    Commented Aug 5, 2018 at 10:00
  • @yimcai How would you go about swapping the coordinates places? Apparently it needs to be (-120.298973, 50.673675), and I wrote the script below for that. For some reason, its not working. Any ideas? for u in gdf['Geolocation']: u = u.replace('(', '') u = u.replace(')', '') a, b = u.split(',') correct = [b, a] correct = gdf['Geolocation_correct'] Commented Aug 6, 2018 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.