Replace Values from One Table with Values from Another?

1228
2
11-27-2019 11:28 AM
IsaiahBarker
New Contributor

Hello,

I’ve given myself a bit of a personal challenge at work to try and create a Python script that can help streamline the process of replacing multiple rows in an attribute table. I have an assignment that requires me to update almost 3,000 entries of data in a table. I could do it manually, but Python seems purpose-built for this kind of thing. I’m hitting a couple of road blocks that I hope can be clarified.

What I’m trying to accomplish is easy in theory: a script that will read the values of a certain column in a table (these are essentially unique identifier numbers), compare them to the values in a column in a second table, and if those values are equivalent, replace them with values from another column. Here's how I'm visualizing it:

Read each row value of column C in table 1

Read each row value of column C in table 2

See if the identifier numbers are exactly the same (==). If so, pluck the row values from column D in table 2 and overwrite table 1, column D's row values with them. Leave any values that do not match alone.

---

Here's how I'd think it works in Python, but I'm rusty and need some help stringing the ideas together:

Import arcpy, Import env

Table2 = "C:/Documents/Table2.csv"

For entry in Table1:

With arcpy.da.UpdateCursor(Table1, ColumnD) as cursor:

If Table2Col == Table1Col:

arcpy.updateRow()

I know this is pretty off, but it's about as far as I can muster at the moment. How do I get this code from theoretical to functional? In particular, how can I get Python to refer to one table's column, compare it to another, and if the row values are equivalent, replace the values in another column? Also, would the Boolean operator help keep any entries that do not match from being altered or deleted?

0 Kudos
2 Replies
RandyBurton
MVP Alum

Have you checked out: Turbo Charging Data Manipulation with Python Cursors and Dictionaries ?

From your description, it sounds like you want to do what is described in the article.

0 Kudos
DougBrowning
MVP Esteemed Contributor

Why not use a join?  It will do that matching for you.  Then cycle through the fields doing a field calc to move the data over.

0 Kudos