Field calculator to calculate diff (row values) in a single column

2752
5
Jump to solution
05-16-2018 05:23 AM
youngpark
New Contributor III

Hello,

I have a column for elevation for a point data set.  I would like to create a new column to show a difference between Row(n) and Row(n+1) values.  well, I assume that the easiest way is calculating in EXCEL (=abs(A1-A2)) then make a join at ArcGIS.  However, I would like to the process with "Field calculator"   Does anyone know how to do it? 

Thanks in advance

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

Added to Table Tools for PRO 

as a field calculator sequence function

View solution in original post

5 Replies
RobertBorchert
Frequent Contributor III

abs([field 1] - [field 2])

youngpark
New Contributor III

Hi Robert.

Value comparison in a SINGLE column.

NOT comparing field 1 and field 2.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Learn GIS ... some python?

Could make this into a script or copy and paste into the python window or your python IDE

# ---- 
# (1) specify the table you want to use, with the OBJECTID field and 
#     the field you want to get sequential differences for
# (2) convert the table to a numpy array... there is some magic in that we 
#     'view' the data as double type (see the vals = line)
# (3) perform the difference, diff, by offsetting and slicing the input values)
# (4) create an output array to put the values in
# (5) 'join' permanently the result (out) to the input using the ExtendTable func
#
# ---- example below was a quick test to calculate sequential difference in 'Xs'

flds = ['OBJECTID', 'Xs']
pth = r"C:\Your_spaceless_path\Your.gdb\Your_table"
tbl = arcpy.da.TableToNumPyArray(pth, flds)
vals = tbl['Xs'].view(np.float64)
#
diff = vals[1:] - vals[:-1]  # ---- changeup the function ----
#
out = np.zeros((tbl.shape[0],), dtype=[('IDs', '<i4'), ('Seq_Diff', '<f8')])
out['IDs'] = tbl['OBJECTID']
out['Seq_Diff'][1:] = diff
arcpy.da.ExtendTable(pth, 'OBJECTID', out, 'IDs')

# ---- Seems like overkill? nope... I just stole the code from a pre-existing
#      snippet, changed the filename, the field of interest and my amazing math function

Obviously study material, but give it a try sometime.

DanPatterson_Retired
MVP Emeritus

Added to Table Tools for PRO 

as a field calculator sequence function

youngpark
New Contributor III

Thanks, Dan,

I really appreciate it

0 Kudos