I am trying to calculate the percentage of a total which the median value makes. I have a column with the median column labelled (i.e. 1st,2nd,3rd,4th column is median), the columns of values and the total of all values. I wrote a small python function in the Codeblock and it returns a value of 0 rather than a percentage. All the fields involved are dobule. I know I did this correctly before but I've tried almost everything to fix my code and cannot get it to return anything other than 0. Here is a screenshot of what I did in field calculator:

enter image description here

And here is the code written out:

def medfind(peri1,peri2,peri3,peri4,peri5,peri6,peri7,index,tot):
      dict = {1:peri1,2:peri2,3:peri3,4:peri4,5:peri5,6:peri6,7:peri7}
      return dict[index]/tot

I have double checked all the fields and everything does have a value. I just can't figure out why it's not working. This is in a .shp by the way, and I have previously used this function to success.


I think Python is running into cases where dict[index] and tot are both integer values (even though they are defined as doubles in the Shapefile). Because of integer division you are ending up with 0 as the answer. For example:

>>> dict = { 1:1 }
>>> tot = 5
>>> dict[1]/tot

To fix it, simply cast the first parameter to a float:

>>> dict = { 1:1 }
>>> tot = 5
>>> float(dict[1]) / tot

So your fixed code will be:

def medfind(peri1,peri2,peri3,peri4,peri5,peri6,peri7,index,tot):
  dict = {1:peri1,2:peri2,3:peri3,4:peri4,5:peri5,6:peri6,7:peri7}
  return float(dict[index])/tot
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.