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?
JavaScript
x
8
1
mask = (svc_df['createdate'] < curdate) & (svc_df['createdate'] >= backdate)
2
print(svc_df.loc[mask])
3
# Detect the sub-dataframe and then assign to a new dataframe
4
sel_df = svc_df.loc[mask]
5
#Create a geodf from alabama services
6
al_gdf = geopandas.read_file(alSvc_shp)
7
al_merge = al_gdf.merge(al_gdf, sel_df, left_on="Location", right_on="sketch_LOC")
8
Advertisement
Answer
- have synthesized a MWE from your code. Generation and data frame and geo data frame
- you have an error:
JavaScript
1
2
1
al_merge = al_gdf.merge(al_gdf, sel_df, left_on="Location", right_on="sketch_LOC")
2
- have used
dataframe.merge()
notpd.merge()
hence only one data frame should be passed as a parameter - full working example below
JavaScript
1
28
28
1
import pandas as pd
2
import numpy as np
3
import geopandas as gpd
4
5
# synthesize
6
svc_df = pd.DataFrame(
7
{
8
"createdate": pd.date_range("1-mar-2022", periods=30),
9
"sketch_LOC": np.random.choice(["CHN", "USA", "IND", "JPN", "DEU"], 30),
10
}
11
)
12
curdate = pd.to_datetime("today")
13
backdate = curdate - pd.Timedelta("5 days")
14
15
mask = (svc_df["createdate"] < curdate) & (svc_df["createdate"] >= backdate)
16
print(svc_df.loc[mask])
17
# Detect the sub-dataframe and then assign to a new dataframe
18
sel_df = svc_df.loc[mask]
19
# Create a geodf from alabama services
20
# al_gdf = geopandas.read_file(alSvc_shp)
21
# synthesize
22
al_gdf = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres")).assign(
23
Location=lambda d: d["iso_a3"]
24
)
25
al_merge = al_gdf.merge(sel_df, left_on="Location", right_on="sketch_LOC")
26
27
28