Select to view content in your preferred language

ArcGIS Pro calculate percentage of a field with field calculator

1317
8
Jump to solution
12-19-2023 08:58 AM
Labels (1)
YusefSamari
Occasional Contributor

I have a table of grid cells with a field for the number of plant taxa recorded in each grid cell. I want to calculate a new field showing the percentage of all plant taxa across all grid cells that have been recorded in each grid cell.

The plant taxa recorded field is of type Long and looks like this:

YusefSamari_0-1703004399485.png

I am adapting the answers to a thread asking this same question here:

https://community.esri.com/t5/python-questions/field-calculator-sum-of-field/m-p/141870/highlight/tr...

For some reason this text box won't let me copy and paste indentation. I am pasting snapshots of my codeblock in two parts:

YusefSamari_2-1703004685588.png

YusefSamari_3-1703004724735.png

The expression is:

YusefSamari_0-1703068442626.png

 

The expression says its valid. I have taken this from copying and pasting an accepted solution in the above thread and just put my own path in there. The field I am creating is of type Double. When I try to calculate, I get the following error message:

ERROR 000539: Traceback (most recent call last):
File "<expression>", line 1, in <module>
File "<string>", line 11, in update
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

 

Can anyone please help me understand what is going wrong?

 

 

 

 

 

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor
8 Replies
DavidPike
MVP Frequent Contributor

I'd guess a float can't be added to your Long field type (integers).  Can you Add a new field and try it on that, or use int( (a/S) * 100 )

0 Kudos
YusefSamari
Occasional Contributor

Hi, thanks for the reply. Sorry, I realised I posted the wrong screenshot for the expression section, I have edited now. It should be:

YusefSamari_0-1703068489766.png

'Plant_Taxa_Percentage' is a Double field, 'Plant_Taxa_Recorded' is a Long field. So I am using the codeblock above to try to populate the Double field with percentage values derived from the Long field. I've tried using int( (a/s) * 100) and get the same error message. I have also tried creating two new Double fields, populating one with the values from the Long field, and then calculating percentage from that in the other Double field, so that it was a Double to Double calculation, but still got the same error message.

I then wondered if the 'if len(list) == 0' bit of the code was causing issues, but I tried a previous accepted answer in the above thread that didn't have this component and still got the same error message...

 

0 Kudos
DavidPike
MVP Frequent Contributor

Do you have the correct scale and precision for the field properties when created?  I'd also check no integer division is going on, so try float(a) float(S) etc.

0 Kudos
YusefSamari
Occasional Contributor

Sorry, what exactly do you mean by 'scale and precision for the field properties'? I can't see any options for those when creating a new field in Fields View or in Field Calculator. The table is a dummy table as I try to figure this out to later put it in a model. It only has 4 rows, and the values of the column I am trying to calculate percentages for are:

1245
515
1112
973

I have just tried running the calculation from two float fields, and also inserting float(a) and float(S) into the final line of my codeblock. Still getting the same error message! Thanks for your help so far.

0 Kudos
DavidPike
MVP Frequent Contributor

Try Add Field geoprocessing tool.  You can specify the precision and scale of the float/double field.  Looking at your code - the del cursor, row looks out of indentation.  I think it should be at the same level as 'with cursor'.  Also try just returning 'S' or 'a' to troubleshoot.
If this is in a model, can you just do summary statistics then get the SUM value from that table to use?  What's the ultimate goal here, as a cursor within a field calculator is very hacky at best.

0 Kudos
YusefSamari
Occasional Contributor

Thanks, I tried using Add Field to create two new Float fields, populated one with the values from the original Long field, then tried to calculate percentages for the other and still got the same error message. The indentation is correct in the codeblock, it just looks slightly off because I had to paste those screenshots in two separate parts. 
Running the codeblock to just return 'S' or 'a' also led to the same error.

The ultimate goal is to make a chart map showing bar charts of how many species have been recorded in each 10km square over a region, for a variety of taxon groups, e.g. birds, plants, butterflies, etc. But I want the axes to be broadly comparable, and there a wildly different numbers of species in each taxon group in the region, e.g. 100s of plants but only 30 butterflies. So I want to show in each square, the percentage of all species in the region that have been recorded in that square, for each taxon group, like the table below but with many more taxon group columns:

10km squarePlant_SpeciesPlant_Species_%
A124532.38
B51513.39
C111228.92
D97325.31

 

I just made this dummy table to try to figure out the calculate percentage part, but am falling down even at this first step! 
How would I incorporate the results of a Summary Statistics into the field calculator? Is there a way to make the output from Summary Statistics into an inline variable in model builder?

 

0 Kudos
DavidPike
MVP Frequent Contributor

https://community.esri.com/t5/modelbuilder-questions/sum-the-values-of-a-field-and-paste-the-sum-in/... shows a pure model builder way.  NB it's just a variable, not an inline variable.

YusefSamari
Occasional Contributor

Thanks so much for taking the time to help here. The pure model builder solution has worked. I still have an itch as to why the other method didn't work, but I'll have to leave that unscratched for now!