Select to view content in your preferred language

How to Concatenate Fields

4523
27
02-06-2013 09:52 AM
CraigMcDade
Occasional Contributor III
I have an attribute table with approx 1000 records. There are two Fields I care about: Name and Pg_Number (see attached pic)

I would like to consolidate the fields so that I received an output or a new field that combines the Pg_Number field based on the Name Field

Ex.

In my head its something like:
if [Name] = "Orange Lake" then [Pg_Number] = 5, 6, 7, 8, 9, 10...
if [Name] = "Green Lake" then [Pg_Number] = 11, 12, 13, 14, 15..."
and so on...


I haven't had much experience with Python but I'm trying to build a model/script and I'm getting stuck on how to do this field calc/consolidation.

Any help would be appreciated.[ATTACH=CONFIG]21511[/ATTACH]
Tags (2)
0 Kudos
27 Replies
CraigMcDade
Occasional Contributor III
1 step closer. Now its telling me:

Runtime error
Traceback (most recent call last):
  File "<string>", line 19, in <module>
RuntimeError: Cannot find field 'NAME'

Does my new table need to have these field names or will it generate them automatically?
0 Kudos
MathewCoyle
Frequent Contributor
You need to create your target table with the field names and properties you want. You could modify the insertCursor object creation to have different field names if you'd like, but yes they will have to created already.
0 Kudos
CraigMcDade
Occasional Contributor III
I figured as much and created them as indicated in the code. Same error.
0 Kudos
MathewCoyle
Frequent Contributor
Post a copy of the table you created.
0 Kudos
CraigMcDade
Occasional Contributor III
I've attached it. Thanks again.
0 Kudos
MathewCoyle
Frequent Contributor
Not sure why you would get that error. Are you sure 50 characters is long enough? I think you would see a different error if that were the issue though...

Actually I think I may have missed the last bracket in the last line I posted.

insertCursor.insertRow((key, ', '.join(str(item) for item in val)))


If that modification nets the same error can you post the code you are running?
0 Kudos
CraigMcDade
Occasional Contributor III
The NAME field doesn't need to fit that many characters, The Pg_Number field needs to be long, I've adjusted it to its maximum size (which is apparently only 254 characters?) However, one of the NAME fields has 74 different page numbers plus the ", " so its maximum size should be set to 370

The code results in the same error, the full code I'm running is:

import os
import arcpy

table = r"c:\users\craig.mcdade\GridforPython.dbf" # source_table
new_table = r"table.dbf" # new table name + extension only
tempTable = r"\\in_memory\temp_table"
outPath = r"c:\users\craig.mcdade" # path to output location on disk
keyField = 'NAME'
valField = 'Pg_Number'

tableDict = {}
arcpy.MakeTableView_management(table, tempTable)
with arcpy.da.SearchCursor(tempTable, [keyField, valField]) as cursor:
    for row in cursor:
        tableDict.setdefault(row[0], []).append(row[1])

insertCursor = arcpy.da.InsertCursor(os.path.join(outPath, new_table), [keyField, valField])
for key, val in tableDict.iteritems():
    insertCursor.insertRow((key, ', '.join(str(item) for item in val)))
0 Kudos
CraigMcDade
Occasional Contributor III
I made the following change to the code and it is working now:

Replaced:

with arcpy.da.SearchCursor(tempTable, [keyField, valField]) as cursor:


with:

with arcpy.da.SearchCursor(table, [keyField, valField]) as cursor:


Thanks for all your help! Now I just need to figure out how to create the field long enough so that all results are populated.
0 Kudos