Select to view content in your preferred language

Using Python to update tables

799
2
Jump to solution
02-01-2024 09:15 PM
JGodd
by
New Contributor

I am working on a project for an online class that uses Python with ArcGIS Pro. This is a beginner class but I am terrible with scripting so everything is a struggle for me. This is the prompt: "Write a Python script that finds the difference between populations for U.S. counties from 1990 to 2010 and writes that information to a new field." This data was given to us to download and import into Pro. 

I have used python to create the new field in the table in Pro, but I have no idea how to take the two other fields that contain the 1990 population and the 2010 population and use them to populate my new field I created in the table.

 

This is the tutorial they gave us to go off of:

# Define the object with which we are working.
cities = "Cities.shp"  # Notice I don't use the whole path if the file is in the workspace.

# Finds city names and coordinates
with arcpy.da.UpdateCursor(cities, ["NAME", "X_CO", "Y_CO" "SHAPE@X", "SHAPE@Y"]) as cursor:
    for row in cursor:
        NAME = row[0]
        x = row[3]
        y = row[4]


# Here we have opened an update cursor to find data inside cities.
# This lets us read and write data to this file.
# "NAME", "X_CO", and "Y_CO" are names of a fields containing city names.
# and blank fields.
# SHAPE@X and SHAPE@Y are tokens to retrieve the X and Y data for each city.
# We have then saved the data to a variable
# Let's populate the X_CO and Y_CO fields with X Y data.

        row[1] = x
        row[2] = y
        cursor.updateRow(row)

 

For me I don't understand how this is the same thing as what the prompt is telling me to do, but again I struggle a lot with writing/understanding scripts. 

I feel like I am missing something very simple that would make all of this click for me. Not looking for someone to just write it out for me, but any direction would be greatly appreciated.

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

You want to take the values from 2 fields and put them into 1 new field?  I don't quite understand, do you want the difference in population?  What exactly don't you understand as there's a few things going on in the script.

 

# Define the object with which we are working.
cities = "NameOfYourData.shp"  # Notice I don't use the whole path if the file is in the workspace.
# could be r'c:\myFiles\data.shp' as man example of an absolute path

# Finds city names and coordinates
#an update cursor allows you to access the data and/or edit existing rows
#with arcpy.da.UpdateCursor(PathToYourData, [names of fields you want to read or edit])
with arcpy.da.UpdateCursor(cities, ["POP2010", "POP2020", "POP_DIFF"]) as cursor:
    for row in cursor:
        #row 0 is the 1st field you specified in the list above i.e. POP2010
        #row 1 is the 2nd i.e. POP2020...
        #set readable variables from these row values 
        pop_2010 = row[0]
        pop_2020 = row[1]
        # calculate value to put into POP_DIFF i.e. row[2]
        pop_diff = pop_2020 - pop_2010
        #now set "POP_DIFF" (i.e. row[2]) field to calculated value above
        row[2] = pop_diff

        #tell the cursor to update your row, then it moves onto 
        #the next row automatically until it reaches the end 
        #and the cursor closes itself.
        cursor.updateRow(row)

View solution in original post

0 Kudos
2 Replies
DavidPike
MVP Frequent Contributor

You want to take the values from 2 fields and put them into 1 new field?  I don't quite understand, do you want the difference in population?  What exactly don't you understand as there's a few things going on in the script.

 

# Define the object with which we are working.
cities = "NameOfYourData.shp"  # Notice I don't use the whole path if the file is in the workspace.
# could be r'c:\myFiles\data.shp' as man example of an absolute path

# Finds city names and coordinates
#an update cursor allows you to access the data and/or edit existing rows
#with arcpy.da.UpdateCursor(PathToYourData, [names of fields you want to read or edit])
with arcpy.da.UpdateCursor(cities, ["POP2010", "POP2020", "POP_DIFF"]) as cursor:
    for row in cursor:
        #row 0 is the 1st field you specified in the list above i.e. POP2010
        #row 1 is the 2nd i.e. POP2020...
        #set readable variables from these row values 
        pop_2010 = row[0]
        pop_2020 = row[1]
        # calculate value to put into POP_DIFF i.e. row[2]
        pop_diff = pop_2020 - pop_2010
        #now set "POP_DIFF" (i.e. row[2]) field to calculated value above
        row[2] = pop_diff

        #tell the cursor to update your row, then it moves onto 
        #the next row automatically until it reaches the end 
        #and the cursor closes itself.
        cursor.updateRow(row)
0 Kudos
JGodd
by
New Contributor

This is what I needed thank you, but I was trying to both create a new field and then fill it with the difference of populations.

Do you have any good online sources or books that you recommend to help with python and GIS. I am graduating in a couple weeks and while I have a good handle on ArcGIS my Python skills are more than lacking. I am hoping to get into the GIS field after graduating so I would really like to improve on this.

Thanks for the help.

0 Kudos