Skip to content
Advertisement

Pandas/Geopandas Merge with a mask selection

I usually work with Arcpy but am trying to learn more pandas/geopandas uses. I have a mask applied to a csv table and a shapefile that I want to merge together in order to find matches between the two based on a specific field.

However, when I try to merge them together, I get the error “The truth value of a Dataframe is ambiguous.” How do I merge a masked dataframe? I’ve included the segment of code below that creates the mask (utilizing two date variables and a date field) and the merge which uses the Location fields (different names on each dataframe).

What do I need to do to manipulate the mask dataframe into functioning in a mask?

    mask = (svc_df['createdate'] < curdate) & (svc_df['createdate'] >= backdate)
    print(svc_df.loc[mask])
    # Detect the sub-dataframe and then assign to a new dataframe
    sel_df = svc_df.loc[mask]
    #Create a geodf from alabama services
    al_gdf = geopandas.read_file(alSvc_shp)
    al_merge = al_gdf.merge(al_gdf, sel_df, left_on="Location", right_on="sketch_LOC")

Advertisement

Answer

  • have synthesized a MWE from your code. Generation and data frame and geo data frame
  • you have an error:
al_merge = al_gdf.merge(al_gdf, sel_df, left_on="Location", right_on="sketch_LOC")
  • have used dataframe.merge() not pd.merge() hence only one data frame should be passed as a parameter
  • full working example below
import pandas as pd
import numpy as np
import geopandas as gpd

# synthesize
svc_df = pd.DataFrame(
    {
        "createdate": pd.date_range("1-mar-2022", periods=30),
        "sketch_LOC": np.random.choice(["CHN", "USA", "IND", "JPN", "DEU"], 30),
    }
)
curdate = pd.to_datetime("today")
backdate = curdate - pd.Timedelta("5 days")

mask = (svc_df["createdate"] < curdate) & (svc_df["createdate"] >= backdate)
print(svc_df.loc[mask])
# Detect the sub-dataframe and then assign to a new dataframe
sel_df = svc_df.loc[mask]
# Create a geodf from alabama services
# al_gdf = geopandas.read_file(alSvc_shp)
# synthesize
al_gdf = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres")).assign(
    Location=lambda d: d["iso_a3"]
)
al_merge = al_gdf.merge(sel_df, left_on="Location", right_on="sketch_LOC")


User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement