An attributes table has 3 fields: ID, Field1, and Field2 and looks something like this:
ID | Field1 | Field2
1. | text......| 10
2. | text......| NULL
3. | text2....| NULL
4. | text2....| 20
5. | text2....| NULL
In this table there is only 1 value in Field2 for each value of Field1, the rest are all NULL
.
And using python in the field calculator in ArcMap I want to calculate the NULL
values so that the values in Field2 are the same across all equal values of Field1 so the resulting attribute table looks like:
ID | Field1 | Field2
1. | text......| 10
2. | text......| 10
3. | text2....| 20
4. | text2....| 20
5. | text2....| 20
This is what I can come up with but it will not change the NULL
values to what it's reading from the populated row. What I'm trying to have it do is read the populated Field2 value (as x
) for a specific Field1 value then overwrite that and all the other Field2 values that have the same corresponding Field1 value with x
.
Pre-Logic Script:
def Calc( field1 , field2 ):
if field1 == "text" and field2 >= 0:
x = field2
else:
x = None
return x
Field2=
Calc( !Field1! , !Field2! )
-
Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format.Midavalo– Midavalo ♦2017年03月04日 04:41:10 +00:00Commented Mar 4, 2017 at 4:41
1 Answer 1
Something like this should work - Finds all the values in Field1 and Field2 and stores them in a dictionary, then looks up that dictionary for the values to calculate the field.
You will need to update "Field1"
and "Field2"
in the cursor with your actual field names, and "MyFC"
with the name of your layer.
Pre-Logic Script:
myDict = {}
with arcpy.da.SearchCursor("MyFC", ["Field1", "Field2"]) as cursor:
for row in cursor:
if row[1] and row[0] not in myDict:
myDict[row[0]] = row[1]
def Calc( field1 , field2 ):
global myDict
if not field2:
x = myDict[field1]
else:
x = field2
return x
Field2=
Calc( !Field1! , !Field2! )
-
I like scripting, but it can be easily done without it. Create layer from original with definition query Is Not Null. Join it to original using 1st field and apply field calculatorFelixIP– FelixIP2017年03月04日 09:01:37 +00:00Commented Mar 4, 2017 at 9:01
Explore related questions
See similar questions with these tags.