2

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 .

1 Answer 1

1

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

1
  • This answered my question!
    – Ofir
    Mar 13, 2022 at 16:10

Your Answer

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

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