2

I want to import a CSV to use as a geodataframe. When I plot it, nothing shows, which makes me think I'm doing something wrong. Here's my code.

import pandas as pd
from geopandas import GeoDataFrame
myfile = 'data.csv'
df = pd.read_csv(myfile)
crs = {'init': 'epsg:4269'}
gdf = GeoDataFrame(df, crs=crs)
gdf.plot()

The 'geometry' column in my CSV contains two rows, which contain LineStrings e.g.

  1. LINESTRING(-117.674411 33.561825,-117.61164 33.589388)
  2. LINESTRING(-117.687375 33.602688,-117.690752 33.604706)
5

You need to ensure you have actual geometry objects in the dataframe, and not only a string representation of it (which is likely since it is imported from csv). You can convert the column with strings (assuming the strings are in WKT or "Well Known Text" format) to actual geometries with:

from shapely import wkt
df['geometry'] = df['geometry'].apply(wkt.loads)

For a full example, see http://geopandas.readthedocs.io/en/latest/gallery/create_geopandas_from_pandas.html#from-wkt-format

  • This deserves a gold medal! – ShellZero Apr 8 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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