Skip to main content
deleted 61 characters in body; edited tags; edited title
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

ArcPro Python3 Add Adding two or more fields based on condition and reclass sum in existing field using ArcPy with ArcGIS Pro

I have a table that contains data that needs to be summed and the result needs to replace an existing value. I only want to sum the values if they are not -9999. The result would be put in the existing Total column.

Here is my table

Total D1 D2
10 5 5
5 -9999 5
5 5 -9999

ImI am using ArcProArcGIS Pro Python 3 Calculate Field on the Total column. I thought that the following would solve my issue, but I believe my condition is not being set properly. My result is only replacing Total with D1. The sum is not occurring.

Expression

calc(!D1!,!D2!)

Code Block

def calc(D1 , D2 ):
if (D1) != -9999:
    return (D1)
elif (D2) != -9999:
    return (D1 + D2)
else:
    return 0 

This code is replacing a VB Script used in arcmap 10.8

a=0
If [d1]>=0 Then
a=a+ [d1]
End If
If [d2]>=0 Then
a=a+ [d2]
End If

Total = a

Any help is appreciated.

Based on the first comment by mikewatt

Is there a better way to write this script? 

I could have up to 8 columns to sum?.

def calc(D1 , D2 ):
if (D1) != -9999 and (D2) != -9999:
    return (D1 + D2)
elif (D1) != -9999 and (D2) == -9999:
    return (D1)
elif (D2) != -9999 and (D1) == -9999:
    return (D2)
else:
    return 0 

ArcPro Python3 Add two or more fields based on condition and reclass sum in existing field

I have a table that contains data that needs to be summed and the result needs to replace an existing value. I only want to sum the values if they are not -9999. The result would be put in the existing Total column.

Here is my table

Total D1 D2
10 5 5
5 -9999 5
5 5 -9999

Im using ArcPro Python 3 Calculate Field on the Total column. I thought that the following would solve my issue but I believe my condition is not being set properly. My result is only replacing Total with D1. The sum is not occurring.

Expression

calc(!D1!,!D2!)

Code Block

def calc(D1 , D2 ):
if (D1) != -9999:
    return (D1)
elif (D2) != -9999:
    return (D1 + D2)
else:
    return 0 

This code is replacing a VB Script used in arcmap 10.8

a=0
If [d1]>=0 Then
a=a+ [d1]
End If
If [d2]>=0 Then
a=a+ [d2]
End If

Total = a

Any help is appreciated.

Based on the first comment by mikewatt

Is there a better way to write this script? I could have up to 8 columns to sum?

def calc(D1 , D2 ):
if (D1) != -9999 and (D2) != -9999:
    return (D1 + D2)
elif (D1) != -9999 and (D2) == -9999:
    return (D1)
elif (D2) != -9999 and (D1) == -9999:
    return (D2)
else:
    return 0 

Adding two or more fields based on condition and reclass sum in existing field using ArcPy with ArcGIS Pro

I have a table that contains data that needs to be summed and the result needs to replace an existing value. I only want to sum the values if they are not -9999. The result would be put in the existing Total column.

Here is my table

Total D1 D2
10 5 5
5 -9999 5
5 5 -9999

I am using ArcGIS Pro Python 3 Calculate Field on the Total column. I thought that the following would solve my issue, but I believe my condition is not being set properly. My result is only replacing Total with D1. The sum is not occurring.

Expression

calc(!D1!,!D2!)

Code Block

def calc(D1 , D2 ):
if (D1) != -9999:
    return (D1)
elif (D2) != -9999:
    return (D1 + D2)
else:
    return 0 

This code is replacing a VB Script used in arcmap 10.8

a=0
If [d1]>=0 Then
a=a+ [d1]
End If
If [d2]>=0 Then
a=a+ [d2]
End If

Total = a

Is there a better way to write this script? 

I could have up to 8 columns to sum.

def calc(D1 , D2 ):
if (D1) != -9999 and (D2) != -9999:
    return (D1 + D2)
elif (D1) != -9999 and (D2) == -9999:
    return (D1)
elif (D2) != -9999 and (D1) == -9999:
    return (D2)
else:
    return 0 
added 40 characters in body
Source Link

I have a table that contains data that needs to be summed and the result needs to replace an existing value. I only want to sum the values if they are not -9999. The result would be put in the existing Total column.

Here is my table

Total D1 D2
10 5 5
5 -9999 5
5 5 -9999

Im using ArcPro Python 3 Calculate Field on the Total column. I thought that the following would solve my issue but I believe my condition is not being set properly. My result is only replacing Total with D1. The sum is not occurring.

Expression

calc(!D1!,!D2!)

Code Block

def calc(D1 , D2 ):
if (D1) != -9999:
    return (D1)
elif (D2) != -9999:
    return (D1 + D2)
else:
    return 0 

This code is replacing a VB Script used in arcmap 10.8

a=0
If [d1]>=0 Then
a=a+ [d1]
End If
If [d2]>=0 Then
a=a+ [d2]
End If

Total = a

Any help is appreciated.

Based on the first comment by mikewatt

Is there a better way to write this script? I could have up to 8 columns to sum?

def calc(D1 , D2 ):
if (D1) != -9999 and (D2) != -9999:
    return (D1 + D2)
elif (D1) != -9999 and (D2) == -9999:
    return (D1)
elif (D2) != -9999 and (D1) == -9999:
    return (D2)
else:
    return 0 

I have a table that contains data that needs to be summed and the result needs to replace an existing value. I only want to sum the values if they are not -9999. The result would be put in the existing Total column.

Here is my table

Total D1 D2
10 5 5
5 -9999 5
5 5 -9999

Im using ArcPro Python 3 Calculate Field on the Total column. I thought that the following would solve my issue but I believe my condition is not being set properly. My result is only replacing Total with D1. The sum is not occurring.

Expression

calc(!D1!,!D2!)

Code Block

def calc(D1 , D2 ):
if (D1) != -9999:
    return (D1)
elif (D2) != -9999:
    return (D1 + D2)
else:
    return 0 

This code is replacing a VB Script used in arcmap 10.8

a=0
If [d1]>=0 Then
a=a+ [d1]
End If
If [d2]>=0 Then
a=a+ [d2]
End If

Total = a

Any help is appreciated.

Is there a better way to write this script? I could have up to 8 columns to sum?

def calc(D1 , D2 ):
if (D1) != -9999 and (D2) != -9999:
    return (D1 + D2)
elif (D1) != -9999 and (D2) == -9999:
    return (D1)
elif (D2) != -9999 and (D1) == -9999:
    return (D2)
else:
    return 0 

I have a table that contains data that needs to be summed and the result needs to replace an existing value. I only want to sum the values if they are not -9999. The result would be put in the existing Total column.

Here is my table

Total D1 D2
10 5 5
5 -9999 5
5 5 -9999

Im using ArcPro Python 3 Calculate Field on the Total column. I thought that the following would solve my issue but I believe my condition is not being set properly. My result is only replacing Total with D1. The sum is not occurring.

Expression

calc(!D1!,!D2!)

Code Block

def calc(D1 , D2 ):
if (D1) != -9999:
    return (D1)
elif (D2) != -9999:
    return (D1 + D2)
else:
    return 0 

This code is replacing a VB Script used in arcmap 10.8

a=0
If [d1]>=0 Then
a=a+ [d1]
End If
If [d2]>=0 Then
a=a+ [d2]
End If

Total = a

Any help is appreciated.

Based on the first comment by mikewatt

Is there a better way to write this script? I could have up to 8 columns to sum?

def calc(D1 , D2 ):
if (D1) != -9999 and (D2) != -9999:
    return (D1 + D2)
elif (D1) != -9999 and (D2) == -9999:
    return (D1)
elif (D2) != -9999 and (D1) == -9999:
    return (D2)
else:
    return 0 
added 332 characters in body
Source Link

I have a table that contains data that needs to be summed and the result needs to replace an existing value. I only want to sum the values if they are not -9999. The result would be put in the existing Total column.

Here is my table

Total D1 D2
10 5 5
5 -9999 5
5 5 -9999

Im using ArcPro Python 3 Calculate Field on the Total column. I thought that the following would solve my issue but I believe my condition is not being set properly. My result is only replacing Total with D1. The sum is not occurring.

Expression

calc(!D1!,!D2!)

Code Block

def calc(D1 , D2 ):
if (D1) != -9999:
    return (D1)
elif (D2) != -9999:
    return (D1 + D2)
else:
    return 0 

This code is replacing a VB Script used in arcmap 10.8

a=0
If [d1]>=0 Then
a=a+ [d1]
End If
If [d2]>=0 Then
a=a+ [d2]
End If

Total = a

Any help is appreciated.

Is there a better way to write this script? I could have up to 8 columns to sum?

def calc(D1 , D2 ):
if (D1) != -9999 and (D2) != -9999:
    return (D1 + D2)
elif (D1) != -9999 and (D2) == -9999:
    return (D1)
elif (D2) != -9999 and (D1) == -9999:
    return (D2)
else:
    return 0 

I have a table that contains data that needs to be summed and the result needs to replace an existing value. I only want to sum the values if they are not -9999. The result would be put in the existing Total column.

Here is my table

Total D1 D2
10 5 5
5 -9999 5
5 5 -9999

Im using ArcPro Python 3 Calculate Field on the Total column. I thought that the following would solve my issue but I believe my condition is not being set properly. My result is only replacing Total with D1. The sum is not occurring.

Expression

calc(!D1!,!D2!)

Code Block

def calc(D1 , D2 ):
if (D1) != -9999:
    return (D1)
elif (D2) != -9999:
    return (D1 + D2)
else:
    return 0 

This code is replacing a VB Script used in arcmap 10.8

a=0
If [d1]>=0 Then
a=a+ [d1]
End If
If [d2]>=0 Then
a=a+ [d2]
End If

Total = a

Any help is appreciated.

I have a table that contains data that needs to be summed and the result needs to replace an existing value. I only want to sum the values if they are not -9999. The result would be put in the existing Total column.

Here is my table

Total D1 D2
10 5 5
5 -9999 5
5 5 -9999

Im using ArcPro Python 3 Calculate Field on the Total column. I thought that the following would solve my issue but I believe my condition is not being set properly. My result is only replacing Total with D1. The sum is not occurring.

Expression

calc(!D1!,!D2!)

Code Block

def calc(D1 , D2 ):
if (D1) != -9999:
    return (D1)
elif (D2) != -9999:
    return (D1 + D2)
else:
    return 0 

This code is replacing a VB Script used in arcmap 10.8

a=0
If [d1]>=0 Then
a=a+ [d1]
End If
If [d2]>=0 Then
a=a+ [d2]
End If

Total = a

Any help is appreciated.

Is there a better way to write this script? I could have up to 8 columns to sum?

def calc(D1 , D2 ):
if (D1) != -9999 and (D2) != -9999:
    return (D1 + D2)
elif (D1) != -9999 and (D2) == -9999:
    return (D1)
elif (D2) != -9999 and (D1) == -9999:
    return (D2)
else:
    return 0 
edited tags; edited tags
Link
Ian Turton
  • 83.2k
  • 6
  • 89
  • 189
Loading
Source Link
Loading