Hello,
I am trying to calculate a field in ArcGIS PRO. The field should contain a constant in every row. This constant will be equal to the maximum of another column in the table.dbf WFr_100.
I can get to the number with this code, but I would like to do it through the field calculator inside Arcgis PRO. Is there a way to do that?
# Import system modules
import arcpy
import numpy
# Set environment settings
arcpy.env.workspace = r"C:\Users\MD \Documents\ArcGIS\Projects\IP\Default.gdb"
input = "WFr_100" arr = arcpy.da.TableToNumPyArray(input, ('CMass'))
x=(arr["CMass"].max())
print(x)
Solved! Go to Solution.
Do the work outside and bring it back in.
Close Pro to make sure any file locks are gone.
Verbose example
from arcpy.da import TableToNumPyArray, ExtendTable
tbl = r"C:\arcpro_npg\npg\Project_npg\npgeom.gdb\sample_10k" # my table
arr = TableToNumPyArray(tbl, ["OID@", "Age"]) # the objectid field and another
arr1 = arr["OID@"] # make oid values
arr2 = arr["Age"] # Get the values you are interested
count = arr2.shape[0] # get the length of the array
mx = np.nanmax(arr2) # ---- now get the max... nanmax accounts for nulls
vals = np.repeat(mx, count) # ---- repeat the maximum "count" times
# create output ---- excitement builds
z = np.empty_like(arr) # build the output array
z.dtype.names = ("OID@", 'Max_Vals') # make your output names
z["OID@"] = arr1 # put the id values in
z['Max_Vals'] = vals # put the max in
ExtendTable(tbl, "OID@", z, "OID@") # magic-orama ... like a join
Now open Pro and open the table to see the results.
I have helper functions that expedite this, but you need to understand the procedure first.
numpy is extremely powerful and I have numerous blogs on its use with arcpy in the Python Blogs... look for me and me before retirement.
Do the work outside and bring it back in.
Close Pro to make sure any file locks are gone.
Verbose example
from arcpy.da import TableToNumPyArray, ExtendTable
tbl = r"C:\arcpro_npg\npg\Project_npg\npgeom.gdb\sample_10k" # my table
arr = TableToNumPyArray(tbl, ["OID@", "Age"]) # the objectid field and another
arr1 = arr["OID@"] # make oid values
arr2 = arr["Age"] # Get the values you are interested
count = arr2.shape[0] # get the length of the array
mx = np.nanmax(arr2) # ---- now get the max... nanmax accounts for nulls
vals = np.repeat(mx, count) # ---- repeat the maximum "count" times
# create output ---- excitement builds
z = np.empty_like(arr) # build the output array
z.dtype.names = ("OID@", 'Max_Vals') # make your output names
z["OID@"] = arr1 # put the id values in
z['Max_Vals'] = vals # put the max in
ExtendTable(tbl, "OID@", z, "OID@") # magic-orama ... like a join
Now open Pro and open the table to see the results.
I have helper functions that expedite this, but you need to understand the procedure first.
numpy is extremely powerful and I have numerous blogs on its use with arcpy in the Python Blogs... look for me and me before retirement.
Spot on, Dan! Thank you.