Search code examples
pythonfieldarcmap

python-in-arcmap, how to query using row ID and look for field values from another feature class


I have a feature class that needs to update , name it as big_file, it has a field name 'ID_old' which we will use to do the query. another field is 'Sum_value', which needs to be filled with sum of a field value of another file.

so I have one list (named list_file) that contains many feature classes, name each one as small_file. name of each file already contains the ID (name it 'ID') that we need to locate. each file has a field named 'field_value'. 'ID' is not a field, just name of feature class.

note that 'ID_old' and 'ID' are of different format, since ID does not contain ' ' or '.', for example:

ID_old : New York, St. Louis  corresponds to
ID: New_York, St_Louis

now need to : fill the Sum_value field of big_file with the field_value sum of each small_file that has the correspondent ID with each row.

eg. one row of big_file has the ID which is 'New York', so in the list_file there is a file whose name contains 'New_York', after we find that file, say sth_New_York_file, we will get the sum of the field 'field_value' and have a result. then go back to big_file and fill the field Sum_value of thw row (ID is 'New York') with the result.

In other words, i am trying to do the query using the ID of rows in original file , looking for correspondent feature class in the list, in that feature class get the sum of the field I want, and update each row using the sum in original file.

Is there any clues? I suppose I need to use UpdateCursor, but don't know how to query and get values.


Solution

  • codes look like:

    for file in list_file:
      outStat="path_for_sum_output"
      arcpy.Statistics_analysis(file,outStat, [["field_value", "SUM"]])
      rowsStat = arcpy.SearchCursor(outStat)
      for row in rowsStat:
         sum=row.getValue('Sum_field_value')
    
      #formate name of file so the format match with field ID_old of the feature class
      namelist=file.split('_')
      myname=' '.join(namelist)
      print myname 
    
      fc='big_file'
      cursor=arcpy.da.UpdateCursor(fc,['Sum_value'], "ID_old = '" +myname+ "'")
      for row in cursor:
          row[0]= sum
          cursor.updateRow(row)
          print row
    

    This solved most of the problem, except for the records in big_file with the ID_old starting with 'St.'-- for these records need to fill manually