Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 9 down vote accepted

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
0

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

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

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
share|improve this answer
    
Thank you very much! That was the problem! –  Emily Oct 4 '11 at 15:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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