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.

In my previous question I wanted to reach a field in the field calculation; however, it was pretty easy to introduce a few more inputs for that function.

Now, I am facing a problem which needs around a hundred inputs. It might be a very naive question but I am wondering if there is a way to introduce an input to the function which is an object of the record type and then by a for loop I can traverse through the fields and sum them up.

My data includes the number of people for each age from 0 to 100 year-old and I want to calculate the number of people less than 30, above 30, less than 50 and above 50. So, having 50 inputs for the function and changing them for each new field is time-consuming. I think there is supposed to be a more efficient way.

share|improve this question
    
I would recommend that you take a closer look at python dictionaries. I believe they can be used for what you want to do. Someone else might be able to give more detailed instructions on how, though. –  Martin Jun 14 '13 at 12:44
    
OK! but the point is that when you want to introduce an input for a function in field calculator it just accepts field names, not a record to iterate on it. I am not insisting to use the field calculator if it could be done in maybe scripting module. –  msc87 Jun 14 '13 at 13:50
add comment

2 Answers 2

Maybe I'm misunderstanding what you're looking for, but wouldn't a Select by Attribute work for this? Select From YourTable Where Age > 30, for example? You could run this, get the count, and update any fields accordingly. Or is that not what you want?

share|improve this answer
    
Where in the SQL query is only performed on one field (or the field introduced in FROM) and if you want to use sum, then it again happens on only one field; however, my information that should be summarized are located in separate fields. Moreover, I am not looking for specific data, I am only want to calculate a new field for all records according other fields. –  msc87 Jun 16 '13 at 12:15
add comment

I haven't ever used the Field Calculator in that way and I'm not sure if what you ask is doable in that interface. You could use the CalculateField_management() tool in a python script which might give you more leeway in accessing the row because your beginning from outside the row and could then pass whatever arguments you want.

If I tried to do something like what your asking, I would use the Python Interpreter and use ListFields(), an UpdateCursor(), maybe some AddField()s to get the results I want. Here is an example. I've made the new fields in the code, but you can make them beforehand and just reference them if you like...

import arcpy
dataset = c:\mydataset.shp #Use your own dataset here
fieldlist = arcpy.ListFields(dataset,"","Integer")
arcpy.AddField_management(dataset,"YOUNG","LONG")
arcpy.AddField_management(dataset,"OLD","LONG")
arcpy.AddField_management(dataset,"NRLY_DEAD","LONG")

rows = arcpy.UpdateCursor()

for row in rows: # Loop through Records
  for field in fieldlist: # Loop through Fields
    if row.getValue(field) == 2:
      row.YOUNG += 1
    elif row.getValue(field) > 2:
      row.OLD += 1
    else:
      row.NRLY_DEAD += 1
  rows.updateRow(row)
del rows

I expect you'll still need to add logic to deal with the different fields that are encountered (expand on the if/else statements, depending on what field name is encountered), but you might find some ideas in there...

share|improve this answer
    
Do you think that I can loop over fields based on the order of them? since I know how they are ordered and I can choose the number of fields instead of their names. –  msc87 Jun 14 '13 at 13:58
1  
Hmm... I played around with that a bit, and I wasn't able to do it satisfactorily. Mainly because the row object does not support indexing and I'm not confident that the ListFields function will keep the field names in order. But there are many more knowledgeable people here than I. –  cndnflyr Jun 14 '13 at 15:01
1  
You could iterate over the list of fields using the index number and call something like row.getValue(fields[i].name) –  Jason Jun 14 '13 at 15:02
1  
With my above comment, you also replace for field in fieldlist: with for i in range(len(fieldlist)): –  Jason Jun 14 '13 at 15:21
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.