Assign values from one column to another conditionally using GeoPandas

geopandaspython

I'm having trouble creating an if else loop to update a certain column in my GeoDataFrame. Here I group by and summarize point counts per zone from points feature class to polygon feature class and I also divide the number of points in each zone to the area of the zone in square miles to create incident per area count. Up to this point everything works as expected that gives me number of incidents per area in a pandas series but when I try to assign a string to an empty column on my polygon feature class using if statement I get

ValueError: The truth value of a Series is ambiguous. Use a.empty,
a.bool(), a.item(), a.any() or a.all().

import geopandas as gpd
import pandas as pd

graf = gpd.read_file(r"E:\PoliceData.gdb", driver='fileGDB', layer="GraffitiIncidents")
pz = gpd.read_file(r"E:\PoliceData.gdb", driver='fileGDB', layer="PatrolZones")

# update the incidents column with the count of incident per zone
pz["INCIDENTS"] = gpd.sjoin(graf, pz, how="inner", predicate="within").groupby(["NAME"]).size().reset_index(name="incidentsPerZone")["incidentsPerZone"].copy()

# declare incident rate as series
incidentRate = pz["INCIDENTS"] / (pz["SHAPE_Area"] / 2589988.11).copy()

This is the if statement I'm trying to use assign a string:

if incidentRate > 15:
    pz["PRIORITY"] = "TOP CONCERN"
elif incidentRate >= 12:
    pz["PRIORITY"] = "HIGH CONCERN"
elif incidentRate >= 6:
    pz["PRIORITY"] = "MEDIUM CONCERN"
else:
    pz["PRIORITY"] = "LOW CONCERN"

Best Answer

You can find here a nice explanation of what that error means.

In your case, use like this:

pz.loc[incidentRate > 15, "PRIORITY"] = "TOP CONCERN"    
pz.loc[(incidentRate < 15) & (incidentRate >= 12), "PRIORITY"] = "HIGH CONCERN"    
pz.loc[(incidentRate < 12) & (incidentRate >= 6), "PRIORITY"] = "MEDIUM CONCERN"    
pz.loc[incidentRate < 6, "PRIORITY"] = "LOW CONCERN"
Related Question