Select to view content in your preferred language

How to calculate values for field using other field's values as values in a dictionary?

1455
3
11-13-2019 09:24 AM
JamesBogart
New Contributor

I am using ArcMap Desktop 10.5. Im trying to use field calculator to return a max value between other fields in the feature based upon a condition. I am not new to python, but I am new to implementing it in Arc.  Using field calculator, it seems as though you can reference that row's value for a particular field by delimiting the field names with '!'. What I would like to do is take the numeric amounts for how many people speak a given language per tract feature and then return a string of the most common language name as long as the language is spoken by at least 5% of the population. An example of a python code that I assumed would work is first having a dictionary with keys as the text string for the language name and dictionary values by referring to that field name with '!'. Then putting this dictionary and a function in the 'Pre-logic Script Code' and then in the field calculator passing in this dictionary into the function to return a string.

Pre-logic Script Code:

Totals = {'Total':!Estimate__Total_!, 'Total only English':!Estimate__Total____Speak_only_English!, 'Spanish' : !Estimate__Total____Spanish____Speak_English_less_than_!, 'French': !Estimate__Total____French__Haitian__or_Cajun____Speak_English_le!, 'German' : !Estimate__Total____German_or_other_West_Germanic_languages____S1!, 'Russian': !Estimate__Total____Russian__Polish__or_other_Slavic_languages__1!, 'Indo European': !Estimate__Total____Other_Indo_European_languages____Speak_Engli1!, 'Korean': !Estimate__Total____Korean____Speak_English_less_than_!, 'Chinese': !Estimate__Total____Chinese__incl__Mandarin__Cantonese_____Speak1!, 'Vietnamese': !Estimate__Total____Vietnamese____Speak_English_less_than_!, 'Tagalog': !Estimate__Total____Tagalog__incl__Filipino_____Speak_English_les!, 'Asian': !Estimate__Total____Other_Asian_and_Pacific_Island_languages____1!, 'Arabic': !Estimate__Total____Arabic____Speak_English_less_than_!, 'Other': !Estimate__Total____Other_and_unspecified_languages____Speak_Eng1!}

def mostCommon (dic)
    most = 0
    lang = ''
    tot = dic['Total']
    for k,v in dic.items():
        if (v > most) and (v >= (tot *.05)):
            most = v
            lang = k
    if lang == '':
        return 'None'
    else:
        return lang

and then in my field calculator box below I simply pass the dictionary into the function

mostCommon(Totals)

However, this gives me a parameter error. Is there any other way for this to be done in field calculator? I have also researched using UpdateCursor, but im not quite sure how I can reference a value in a particular field for the row and add it to a dictionary as I have above.

thank you for your help

0 Kudos
3 Replies
JoshuaBixby
MVP Esteemed Contributor

This seems more fit for cursors than Field Calculator.  I suggest taking a look at /blogs/richard_fairhurst/2014/11/08/turbo-charging-data-manipulation-with-python-cursors-and-diction...

JamesBogart
New Contributor

Thank you , this led me on the right path. I solved me issue by using a standard update cursor and not the data access type, and adding the field values to my dictionary by using the row.getValue('fieldname') within the row iteration and then calling my function in the setValue method.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I am glad you got it working, but the old/original ArcPy cursors (the ones you appear to use) are much slower and inefficient compared to the ArcPy Data Access cursors.  If you are interested in using cursors more in the future, I encourage you to use the ArcPy DA cursors.