Select to view content in your preferred language

How to Iterate over a list of fields and use an UpdataCursor to update the values?

848
5
03-07-2013 12:45 PM
PeterWilson
Frequent Contributor
I have multiple feature class that I performed analysis on and for each field that starts with a "t*" and is type "Double". I need to use a UpdateCursor to update the values within each of the following fields. I don't know how to use a list of fields within my UpdateCursor as the field that needs to be updated. In a previous python script that I used to carry out summary statistics I used a python list to summarise the fields that started with a "t*" and were of type "Double". I'm not sure how to combine the two to get the Update Cursor to replace the first field for each update. Any help would really be appreciated.

>>>import arcpy
... fc = r'E:\Projects\H109009\Calcs.gdb\Int_NLC_Quats_CommAgricMD'
... cursor = arcpy.da.UpdateCursor(fc,["t75c2","Shape_Area","MD_NLC_Area"])
... for row in cursor:
...     row[0] = (row[0]*(row[1]/row[2]*100))/100
...     cursor.updateRow(row)


Regards
Tags (2)
0 Kudos
5 Replies
MathewCoyle
Honored Contributor
You can just get a field list and add it to your base field list you want for every calculation. You can give something like this a try.

base_list = ["Shape_Area","MD_NLC_Area"]
f_list = [f.name for f in arcpy.ListFields(fc, 't*', 'DOUBLE')]
final_list = f_list + base_list
cursor = arcpy.da.UpdateCursor(fc, final_list)


Or if you just want to take the first 't*' field you can do this.
t_field = arcpy.ListFields(fc, 't*', 'DOUBLE')[0].name
0 Kudos
PeterWilson
Frequent Contributor
Hi Mathew

Thanks for the following. The problem that I have is replacing row[0] with the next field in the list that starts with "t". To give you a bit more background. I've intersected shapefiles and the I've over 150 fields with commercial agriculture values (numeric) and each field has a number like "t75c2"; "t75c3" etc. I need to apply the following  formula to each field that starts with a "t" within my feature class.

Thanks Peter
0 Kudos
MathewCoyle
Honored Contributor
Ah sorry I misunderstood, I thought you had a list of feature classes with various t* fields. Could you provide a sample schema of what you are working with?

Based on what you've posted this might get you going, but it is hard to code without knowing your fields you are working with.
field_list = ['t75c', 'SHAPE@AREA', 'MD_NLC_Area']
t_list = [t for t in field_list if t.startswith('t')]
with arcpy.da.UpdateCursor(fc, field_list) as cursor:
    for row in cursor:
        for index, field in enumerate(t_list):
            row[index] = (row[index]*(row[-2]/row[-1]*100))/100
        cursor.updateRow(row)
0 Kudos
PeterWilson
Frequent Contributor
Hi Mathew

Thanks for all your help, I finally came up with the following with the examples that you had given me.

import arcpy
... fc = r'E:\Projects\H109009\Calcs.gdb\Int_NLC_Quats_CommAgricMD'
... f_list = [f.name for f in arcpy.ListFields(fc, "t*", "Double")]
... field_list = f_list + ["SHAPE@AREA","MD_NLC_Area"]
... cursor = arcpy.da.UpdateCursor(fc,field_list)
... field_count = len(f_list)
... for row in cursor:
...     shape_area = row[-2]
...     md_nlc_area = row[-1]
...     for index in xrange(field_count):
...         row[index] = row[index]*(shape_area/md_nlc_area*100)/100
...     cursor.updateRow(row)


Regards
0 Kudos
KundanDhakal
Deactivated User
Hello Peter,


I need to figure out way to iterate interpolation using fields in my shapefile. Could you please provide an example where you would use python script (ArcGIS 10.2) to iterate interpolation (IDW)? Also, the output raster needs to be with the name as the field used in interpolation, for e.g field ABC12 would result in raster of raster_ABC12.

I would greatly appreciate any help or suggestions..

--kundan


I have multiple feature class that I performed analysis on and for each field that starts with a "t*" and is type "Double". I need to use a UpdateCursor to update the values within each of the following fields. I don't know how to use a list of fields within my UpdateCursor as the field that needs to be updated. In a previous python script that I used to carry out summary statistics I used a python list to summarise the fields that started with a "t*" and were of type "Double". I'm not sure how to combine the two to get the Update Cursor to replace the first field for each update. Any help would really be appreciated.

>>>import arcpy
... fc = r'E:\Projects\H109009\Calcs.gdb\Int_NLC_Quats_CommAgricMD'
... cursor = arcpy.da.UpdateCursor(fc,["t75c2","Shape_Area","MD_NLC_Area"])
... for row in cursor:
...     row[0] = (row[0]*(row[1]/row[2]*100))/100
...     cursor.updateRow(row)


Regards
0 Kudos