Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

How do I add in multiple fields in the attribute table? In the attribute table I have crop types such as corn, cotton, sorghum....etc (as shown image below), in square meters. and I want to generate new fields so I can calculate into acres. I am using python that would have add fields, but I would still have to provide field names, one by one (and there is a lot of field names to enter). Can I just have GIS to generate fields that are based on the existing fields without me list them?

Image of table is attached.

enter image description hereThanks!

share|improve this question
add comment

1 Answer

You'll have to loop through your current fields with ListFields. Then in your AddField_management() call just add '_acres' or something that identifies it as different from your current field name.

Something along these lines:

for field in arcpy.ListFields(featureClass):
    arcpy.AddField_management(featureClass, field.name + '_acres', 'FLOAT')

If you don't want to add a new field for every field in your feature class, you'll have to add some simple conditional statements:

for field in arcpy.ListFields(featureClass):
    if field.name == 'badField': # Name of field you don't want to add acreage for
        print "Not going to add this field"
    else:
        arcpy.AddField_management(featureClass, field.name + '_acres', 'FLOAT')
share|improve this answer
2  
Create a list of fields to be skipped skiplist=["skip1","skip2"] then use if field in skiplist: for the if statement –  mhoran_psprep Jul 12 '12 at 17:20
    
field + '_acres' does not work.... is there other ways I can rename the existing field name? –  asianpenguin Jul 12 '12 at 17:34
    
Sorry -- listing the fields only gives you the location in memory. If you print field in the loop you'll get something crazy. Try field.name (I changed the code in my answer). –  Roy Jul 12 '12 at 17:39
    
You might also consider adding multiple fields with one arcpy statement by unpacking tuples: gis.stackexchange.com/questions/57380/… –  Alex Tereshenkov Oct 30 '13 at 12:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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