Python Forum

Full Version: Iterating an attribute table using Arcpy
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have 4 columns(ID_TRC, LENGTH, Categorie, Score) my code is supposed to iterate through the attribute table and for each value of ID_TRC, if the value if unique, assign New_Score = Score, and if the value is repeating, comparing its corresponding values for LENGTH and Categorie as in the code below for each record of ID_TRC.
finally, a new table is to be created with just ID_TRC and New_Score(I written the code for this part yet.)

from tkinter import *
import arcpy

field_name = "Norm Score"
field_type = "FLOAT"
fc = "C:/Users/umroot/Desktop/tersoo/GIS_data/GUI_CODES/output/LandUse_Roads.shp"
field = ['ID_TRC','LENGTH', 'Categorie', 'Score']

with arcpy.da.UpdateCursor(fc, field) as cursor:
    for row in cursor:
        ID_TRC = row[0]
        LENGTH = row[1]
        Categorie = row[2]
        Score = row[3]
        ScoreSum = 0.0
        LengthSum = 0.0
        New_score= 0.0
        #rowCur = row
        #rowPost = cursor.next()

        with arcpy.da.SearchCursor(fc, field) as incursor:
            for rows in incursor:
                if ID_TRC == rows[0] and LENGTH != rows[2] and Categorie != rows[1]:
                    ScoreSum += Score*LENGTH
                    LengthSum += LENGTH
                    New_score = float(ScoreSum/LengthSum)
                    print("same ID but different lengths and Category",ID_TRC,New_score)

                elif ID_TRC == rows[0] and LENGTH == row[2] and Categorie != rows[1] :
                   ScoreSum += Score*LENGTH
                   New_score = float(ScoreSum/LENGTH)
                   print("same ID but equal length different category",ID_TRC, New_score)

                elif ID_TRC == rows[0] and LENGTH != row[2] and Categorie == rows[1] :
                   ScoreSum += Score*LENGTH
                   New_score = float(ScoreSum/LENGTH)
                   print("same ID but equal length different category",ID_TRC, New_score)

                elif  ID_TRC != rows[0]:
                    New_score = Score
                    print("Different ID",ID_TRC, New_score)

    cursor.updateRow(row)
Please help,....thanks
That's odd, still no ideas or suggestions?? Cry
Hallo teflon,

maybe you could explain it better. Just by reading your text, I do not understand what the problem is or what errors are you getting.
It seems that your question is about arcpy, so the number of answerers is more limited here than for example a random homework question. Add to that, I ignore posts when I see imports for GUI code, because I expect that the asker could have put in more effort to simplify their question and I'm lazy.

If your question isn't about either module, I recommend re-framing your question without any imports.
teflon Wrote:Hello baquerik,
I am working with arcgis and I have an input attribute table with 4 columns{ID_TRC, Length, Category, Score} and an output attribute {ID_TRC, New_score}. My code that is supposed to iterate the ID_TRC column, if an ID value is unique, then assign the Score of table 1 to New_score of table 2. if the value of ID_TRC isn't unique, then depending on the value of category and Length, calculate New_score. my python code is below
[python]import arcpy
from arcgis.gis import GIS

fc = "GIS_data/GUI_CODES/output/LandUse_Roads.shp"
#my_gis = GIS()
#m=my_gis.map()
#m.add_layer(fc,None)
#Include all fields that are used by either the Outer UpdateCursor or Inner SearchCursor
fields = ['ID_TRC', 'LENGTH', 'Categorie', 'Score']


#Outer UpdateCursor for iterating {row}'s
with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:        # these are variables holding individual field values from {row}
        m_id_trc = row[0]
        m_length = row[1]
        m_categorie = row[2]
        m_score = row[3]


        # these are variables for aggregate results from the conditional logic in sub_cursor
        # these will be reset for each new Outer {row}
        # these will NOT be reset for each new Inner {sub_row}
        ScoreSum = 0.0
        LengthSum = 0.0
        New_Score = 0.0 # Aggregate result we wish to update on {row} after iterating all {sub_row}'s

        #Inner SearchCursor for iterating {sub_row}'s
        #Here we use a {where-clause} to limit the record set to records matching ID_TRC on the Outer {row}
        with arcpy.da.SearchCursor(fc, fields, "ID_TRC = " + str(m_id_trc)) as sub_cursor:
            for sub_row in sub_cursor:
                # these are variables holding individual field values from {sub_row}
                sub_id_trc = sub_row[0]
                sub_length = sub_row[1]
                sub_categorie = sub_row[2]
                sub_score = sub_row[3]

                #Aggregate Logic
                #I suspect this is not the correct logic because it is dependent on {sub_row} order!
                if m_id_trc == sub_id_trc and m_length != sub_length and m_categorie != sub_categorie:
                        ScoreSum += sub_score*sub_length
                        LengthSum += sub_length
                        New_Score = ScoreSum/LengthSum
                        print("different lengths",m_id_trc,New_Score)
                elif m_id_trc == sub_id_trc and m_length == sub_length and m_categorie != sub_categorie:
                    #Equal Length
                       ScoreSum += ScoreSum
                       New_Score = ScoreSum/2
                       print("equal length",m_id_trc, New_Score)
                else: #Equal categorie
                    New_Score = m_score
                    print("unique ID",m_id_trc, New_Score)
                #End if
            #End for sub_row
        #End with sub_cursor
        #row[5] = New_Score
        cursor.updateRow(row)
[/python]
Output:
unique ID 1260912 0.800000011920929 unique ID 1381209 0.800000011920929 unique ID 1381210 0.800000011920929 unique ID 1381211 0.800000011920929 unique ID 1381212 0.800000011920929 unique ID 1381213 0.8999999761581421 equal length 1381213 0.0 different lengths 1381213 0.800000011920929 equal length 1381213 0.0 unique ID 1381213 0.800000011920929 unique ID 1381213 0.800000011920929 different lengths 1381213 0.8999999761581421 unique ID 1381213 0.800000011920929 unique ID 1381213 0.800000011920929 unique ID 1381214 0.800000011920929 unique ID 1381215 0.8999999761581421 unique ID 1381215 0.8999999761581421 different lengths 1381215 0.800000011920929 unique ID 1381215 0.8999999761581421 unique ID 1381215 0.8999999761581421 equal length 1381215 0.0 different lengths 1381215 0.8999999761581421 equal length 1381215 49.614176849073125 unique ID 1381215 0.800000011920929 unique ID 1381217 0.8999999761581421 unique ID 1381220 0.800000011920929 unique ID 1381221 0.800000011920929 unique ID 1381222 0.800000011920929 unique ID 1381223 0.800000011920929 unique ID 1381224 0.800000011920929 unique ID 1381225 0.800000011920929 unique ID 1381226 0.800000011920929 unique ID 1381227 0.800000011920929 unique ID 1381228 0.800000011920929 unique ID 1381229 0.800000011920929 unique ID 1381230 0.800000011920929 unique ID 1381231 0.800000011920929 unique ID 1381232 0.800000011920929 unique ID 1381233 0.800000011920929 unique ID 1381234 0.800000011920929 unique ID 1381235 0.800000011920929 unique ID 1381237 0.800000011920929 unique ID 1381238 0.800000011920929 unique ID 1381239 0.800000011920929 equal length 1381239 0.0 equal length 1381239 0.0 unique ID 1381239 0.8999999761581421 unique ID 1260896 0.800000011920929 unique ID 1260897 0.800000011920929 unique ID 1260898 0.800000011920929 unique ID 1260899 0.800000011920929 unique ID 1260900 0.800000011920929 unique ID 1260901 0.800000011920929 unique ID 1260902 0.800000011920929 unique ID 1260903 0.800000011920929 equal length 1260903 0.0 equal length 1260903 0.0 unique ID 1260903 0.8999999761581421 unique ID 1260904 0.800000011920929 unique ID 1260905 0.800000011920929 unique ID 1260906 0.800000011920929 unique ID 1260907 0.800000011920929 unique ID 1260908 0.800000011920929 unique ID 1260909 0.800000011920929 unique ID 1260910 0.800000011920929 unique ID 1260885 0.800000011920929 unique ID 1260886 0.800000011920929 unique ID 1260887 0.800000011920929 unique ID 1260889 0.800000011920929 unique ID 1260890 0.800000011920929 unique ID 1260895 0.800000011920929 unique ID 1390670 0.800000011920929 unique ID 1390671 0.800000011920929 unique ID 1390672 0.800000011920929 unique ID 1390674 0.800000011920929 unique ID 1390675 0.800000011920929 unique ID 1390676 0.6000000238418579 equal length 1390676 0.0 unique ID 1390676 0.6000000238418579 equal length 1390676 0.0 unique ID 1390676 0.20000000298023224 different lengths 1390676 0.6000000238418579 unique ID 1390676 0.6000000238418579 different lengths 1390676 0.20000000298023224 unique ID 1390676 0.6000000238418579 unique ID 1390677 0.6000000238418579 unique ID 1390678 0.6000000238418579 unique ID 1390679 0.6000000238418579 unique ID 1390680 0.6000000238418579 unique ID 1390681 1.0 equal length 1390681 0.0 equal length 1390681 0.0 unique ID 1390681 0.800000011920929 unique ID 1410560 0.6000000238418579 unique ID 1410561 0.6000000238418579 unique ID 1410562 0.6000000238418579 unique ID 1410563 0.6000000238418579 unique ID 1410564 0.6000000238418579 unique ID 1410565 0.6000000238418579 unique ID 1410566 0.6000000238418579 unique ID 1410567 0.6000000238418579 unique ID 1410568 0.6000000238418579 unique ID 1410569 0.6000000238418579 unique ID 1410578 0.6000000238418579 unique ID 1410579 0.6000000238418579 unique ID 1410580 0.6000000238418579 unique ID 1410581 0.6000000238418579 unique ID 1410582 0.6000000238418579 unique ID 1410583 0.6000000238418579 unique ID 1410584 0.6000000238418579 unique ID 1410585 0.6000000238418579 unique ID 1410586 0.6000000238418579 unique ID 1410587 0.6000000238418579 unique ID 1410556 1.0 unique ID 1410557 1.0 unique ID 1410558 0.6000000238418579 unique ID 1410559 0.6000000238418579 unique ID 1490482 0.6000000238418579 unique ID 1625336 0.6000000238418579 unique ID 1625186 1.0 equal length 1625186 0.0 equal length 1625186 0.0 unique ID 1625186 0.6000000238418579 unique ID 1260891 0.800000011920929 unique ID 1260894 0.800000011920929 unique ID 1410571 0.6000000238418579 unique ID 1410572 0.6000000238418579 unique ID 1410589 0.6000000238418579 unique ID 1410658 0.6000000238418579 different lengths 1410658 1.0 unique ID 1410658 0.6000000238418579 unique ID 4012278 0.800000011920929 unique ID 4006492 1.0 equal length 4006492 0.0 unique ID 4006492 1.0 different lengths 4006492 0.6000000238418579 equal length 4006492 0.0 unique ID 4006492 0.6000000238418579 different lengths 4006492 1.0 unique ID 4006492 0.6000000238418579 unique ID 4006873 1.0 equal length 4006873 0.0 equal length 4006873 0.0 unique ID 4006873 0.800000011920929 unique ID 4006872 1.0 unique ID 4008162 0.800000011920929 unique ID 4008163 0.800000011920929 unique ID 4009991 1.0 different lengths 4009991 0.699999988079071 unique ID 4009991 1.0 different lengths 4009991 0.699999988079071 different lengths 4009991 1.0 unique ID 4009991 0.699999988079071 equal length 4009991 167.893988146 unique ID 4009991 0.699999988079071 unique ID 4009991 1.0 equal length 4009991 0.0 unique ID 4009991 1.0 different lengths 4009991 0.699999988079071 different lengths 4009991 1.0 unique ID 4009991 0.699999988079071 different lengths 4009991 1.0 unique ID 4009991 0.699999988079071 unique ID 4009992 0.699999988079071 different lengths 4009992 1.0 different lengths 4009992 0.9000000059604645 different lengths 4009992 0.699999988079071 unique ID 4009992 1.0 equal length 4009992 33.46334929587255 different lengths 4009992 0.699999988079071 equal length 4009992 33.46334929587255 unique ID 4009992 0.800000011920929 unique ID 4009812 1.0 equal length 4009812 0.0 equal length 4009812 0.0 unique ID 4009812 0.6000000238418579 unique ID 4009813 1.0 equal length 4009813 0.0 equal length 4009813 0.0 unique ID 4009813 0.6000000238418579 unique ID 1410657 0.6000000238418579 unique ID 1411109 0.6000000238418579 different lengths 1410658 0.6000000238418579 unique ID 1410658 1.0 equal length 1410658 32.11960875857853 unique ID 1410658 0.6000000238418579 equal length 1410658 0.0 unique ID 1410658 0.6000000238418579 unique ID 4006492 1.0 different lengths 4006492 0.6000000238418579 unique ID 4006492 1.0 equal length 4006492 16.33057645303878 different lengths 4006492 1.0 unique ID 4006492 0.6000000238418579 equal length 4006492 27.2176263402 unique ID 4006492 0.6000000238418579
The output ID_TRC isnt unique, and the value of the new_score is not correct. The table should have two columns, one for the ID_TRC which is suppose to unique and its corresponding new score or old score as the case may be.
a sample of the table is
İmage



Thanks
1.- The image that you added at the end does not load.

2.- I have trouble understanding why would update a data table comparing it to itself.

3.- The if statement that you have created will not tell you if an id is unique (understanding that by unique you mean the only row with that combination of id + length + categorie, maybe that is not what you mean)

4.- The usage of ANDs like that can lead to some values not matching the conditions and ending up in the last else even if you did not want it that way. Imagine these 2 rows:

ID: 001 Lenght: 1 Categorie: 1
ID: 001 Lenght: 2 Categorie: 1

It would skip your first check because not both lenght AND categorie are different:
if m_id_trc == sub_id_trc and m_length != sub_length and m_categorie != sub_categorie:

It would skip the second check because lenght does not match:
if m_id_trc == sub_id_trc and m_length == sub_length and m_categorie != sub_categorie:

So it would end up in Unique. Is that what you want?

5.- If you are doing a search with the ID_TRC why do you add the comparison "m_id_trc == sub_id_trc" later? It seems that it will already match every single time.
Thanks, Baquerik for taking time out to look at my problem. I would appreciate it if you could please give me your email address to enable me to send you the screenshots and a copy of the shapefiles I am currently working with. It will help clarify the problem.