1

I'm new to coding and I need to somehow use an Update Cursor that I can convert the rates of crashes in a block group into 3 separate categories. The crash rate is the difference between the number of crashes in a block group divided by sum of all road lengths in the block group. The update cursor needs to go through my crash_rate field and then place them in my crash_category field. Category 1 is low, category2 is medium, and category 3 is high. My Max crash rate is 373.35998535, my average / mean crash rate is 51.8848051, and my lowest crash rate is 0. Here's the code that I used to find the Max, Min, and Mean:

import arcpy
import sys
import os

fc = r"C:\GISAutomation\Project\GISAutoProject\GISAutoProject.gdb\F2F_BG_crash"

#field = "crash_ratio"
num_sum = 0
num_counter = 0
maximum = 0
minimum = 0
#cursor = arcpy.da.SearchCursor(fc,["crash_ratio"])
if arcpy.Exists(fc):
    print("exists")
with arcpy.da.SearchCursor(fc,["crash_ratio"]) as cursor:
    for row in cursor:
        num_sum = num_sum + row[0]
        num_counter = num_counter + 1
        if row[0]> maximum:  # doing the comparisons to find the maximum value
            maximum = row [0]
        if row[0]< minimum:
            minimum = row [0]
num_average = round(num_sum / num_counter,2)
print ("The average per blockgroup is", num_average, "miles")
print ("The minimum per blockgroup is", round(minimum,3), "miles" )
print ("The maximum per blockgroup is", round(maximum,2), "miles")
print ("script 1 finished")

Does anyone have any advice about how I can set my update cursor to place the different values into either high, medium, or low categories? I'm really lost.

closed as off-topic by Vince, Jochen Schwarze, whyzar, BBG_GIS, Erik Apr 17 at 8:52

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – Vince, Jochen Schwarze, whyzar, BBG_GIS, Erik
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Your code doesn't attempt to do the task about which you are asking. Please Edit the question to remove all the SearchCursor code and instead contain your breakpoints for Low/Medium and Medium/High, the mechanism that assures that the target field exists, and your UpdateCursor with/for loop that does the actual updates (the documentation has examples of the latter two). You then need to provide sample output and a description of what isn't working, along with any error messages. I expect that you will actually be able to get this running without our help. Just keep plugging away at it. – Vince Apr 13 at 13:44
0

If you have calculated the crash ratio as a field then you can use pandas.cut to bin data by intervals.

Code below will add low, medium or high to a category field based on the value of crash ratio:

import arcpy
import pandas as pd

fc = r"C:\GISAutomation\Project\GISAutoProject\GISAutoProject.gdb\F2F_BG_crash" #Change to match your data
ratiofield = 'crash_ratio' #Change
categoryfield = 'crash_cat' #Change

#Adjust:
crash_rate_bins = [0,50,100,500] #0-50, 50-100, 100-500
labels = ['low','medium','high'] #Must match number of bins above. This is what is written to categoryfield.

#Create a pandas dataframe using da.SearchCursor and calculate the bin/category column
df = pd.DataFrame.from_records(data=arcpy.da.SearchCursor(fc, ratiofield), columns=['ratio'])
df['crashclass'] = pd.cut(df['ratio'], bins=crash_rate_bins, labels=labels)

#Write to fc using da.UpdateCursor
give_class = iter(df.crashclass)
with arcpy.da.UpdateCursor(fc, categoryfield) as cursor:
    for row in cursor:
        try:
            row[0] = next(give_class)
            cursor.updateRow(row)
        except StopIteration:
            break

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