Geopandas – How to Fill NaN Geometries Records with Another Geometric Column

geodataframegeometrygeopandasmissing datapython

I have a GeoDataFrame with two geometry columns.
I want to fill missing values of the one with the other.
Both columns contain polygons or multipolygons.
I have tried:

geo_df['geom_2'].fillna(geo_df['geom_1'], inplace=True) 

But an error was raised:
NotImplementedError: fillna currently only supports filling with a scalar geometry

Later, I tried:

geo_df['geom_2'].replace('None', geo_df['geom_1'], inplace=True)

and got the same error.

is there any possible solution for this task?
I'm using GeoPandas verision 0.10.2 .

Best Answer

Using a mask and assignment you can achieve this:

gdf.loc[gdf["geom_2"].isna(), "geom_2"] = gdf["geom_1"]

Full MWE:

import geopandas as gpd
import numpy as np
import random
import shapely

# create a MWE data set
gdf = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres")).loc[
    lambda d: (d["continent"] == "Europe")
    & (~d["iso_a3"].isin(["-99", "RUS"]))
    & (d.geom_type == "Polygon")
].head(8)

# create columns as per question with some nan geometries
gdf["geom_2"] = (
    gdf["geometry"]
    .exterior.apply(
        lambda g: shapely.geometry.Point(g.coords[random.randint(0, len(g.coords)) - 1])
    )
    .sample(int(len(gdf) * 0.75))
)
gdf["geom_1"] = (
    gdf["geometry"]
    .exterior.apply(
        lambda g: shapely.geometry.Point(g.coords[random.randint(0, len(g.coords)) - 1])
    )
)

# keep a record of what started nan
gdf["started_nan"] = gdf["geom_2"].isna()
# now fillna, use a mask and assignment
gdf.loc[gdf["geom_2"].isna(), "geom_2"] = gdf["geom_1"]

gdf