Anyone have any ideas how to calculate a field so the values (going down the table) are
1
2
3
1
2
3
1
2
3
etc. to the end of the table?
These values will end up being color codes for rendering. The actual values will be from 0-9, but need some way to get the repeating values in. Another option might be to calculate random values instead of repeating values. Any thoughts?
Solved! Go to Solution.
If you are running my code... then
seq_num_max(3) ... notice you put the number in not ColorCode... You are calculating ColorCode using ColorCode, not the maximum number that you want
import arcpy
import pandas
repeat_vals = [1, 2, 3]
df = pandas.DataFrame(range(100), columns=['col1'])
df = df.join(pandas.DataFrame(repeat_vals * (len(df)/len(repeat_vals)+1), columns=['col2']))
print df.values
As an alternative to the field calculator, this is a mix of libraries and ultimately simply uses a dictionary and arcpy.da.UpdateCursor to calculate the field with the repeating values.
(really, I had been looking for a reason to dive into using a dictionary to update with an UpdateCursor and this was a good exercise to nail that down).
import arcpy
import pandas
import numpy as np
import sys
#set the repeating values to use
repeat_arr = [1, 2, 3]
#the feature class to be updated, the field to calculat and a join field
fc = r'H:\DescribeFGDB\Readme.gdb\RptLyr'
updateField = 'calcIt'
idField = 'OBJECTID'
#convert the feature class to an array with OID values
nparr = arcpy.da.FeatureClassToNumPyArray(fc, ['OID@'])
#get array as pandas dataframe and populate the repeating values column
df = pandas.DataFrame(nparr)
df = df.join(pandas.DataFrame(repeat_arr * (len(df)/len(repeat_arr)+1), columns=['calcIt']))
#convert back to a numpyarry
x = np.array(np.rec.fromrecords(df.values))
names = df.dtypes.index.tolist()
x.dtype.names = tuple(names)
#convert to an in_memory table, set join field and the field to use to calculate with
arcpy.da.NumPyArrayToTable(x, r'in_memory\finalarr')
joinTab = r'in_memory\finalarr'
joinIdField = 'OBJECTID'
joinValueField = 'calcIt'
#create dictionary from the in_memory table
valueDic = dict ([(key, val) for key, val in arcpy.da.SearchCursor(joinTab, [joinIdField,joinValueField])])
#update the source feature class with the dictionary values, joining on the joinfields
with arcpy.da.UpdateCursor(fc, [updateField, idField]) as cursor:
for update, key in cursor:
if not key in valueDic:
continue
row = (valueDic [key], key)
cursor.updateRow(row)
del cursor