Search code examples
pythonpandasgeopandas

How do you store a tuple in a geopandas geodataframe?


I have

blocks["centroid"] = blocks.centroid

doing that will store a shapely.Point in blocks["centroid"] but the shp format does not accept two geometries at the time of blocks.to_file(). So I'm trying to save the values as, maybe a tuple or a list, but merely doing

blocks["centroid"] = (blocks.centroid.x, blocks.centroid.y)

won't work, throwing.

Length of values does not match length of index

how should I store the lat/lon of the point in a single column?


Solution

  • How do you store a tuple in a geopandas geodataframe?

    blocks['centroid'] = blocks.apply(
        lambda row: (row['geometry'].centroid.x,
                     row['geometry'].centroid.y), axis=1)
    
    

    results:

    > blocks['centroid']
    0       (3.74932619950664, -3.54174728953859)
    1      (3.83665124133256, -3.556156891148994)
    ..
    

    but this will not let you use .to_file() to save as ESRI Shapefile. Because ESRI Shapefile does not support tuple nor list.

    how should I store the lat/lon of the point in a single column and save it as ESRI Shapefile?

    ESRI Shapefile supports these types only. We can save the centroid coordinates as a concatenated string.

    blocks['centroid'] = blocks.apply(
        lambda row: '{},{}'.format(row['geometry'].centroid.x,
                                   row['geometry'].centroid.y), axis=1)
    
    blocks.to_file(filename='blocks', driver='ESRI Shapefile')
    

    results

    > blocks['centroid']
    0       3.74932619950664,-3.54174728953859
    1      3.83665124133256,-3.556156891148994
    ..