Calculate fields using first two words from another field arcgis pro

2179
6
05-30-2020 05:45 PM
JodieRobertson
New Contributor

I am trying to use the field calculator to populate a new field with just the first two words from another field.

I can only get the first word, or the second word, but not both to populate the new field. Where am I going wrong with the following code?

!NEWFIELD!.split(" ")[0] + "_" + !NEWFIELD!.split(" ")[1]

ERROR 000539: Traceback (most recent call last): File "<expression>", line 1, in <module>IndexError: list index out of range

0 Kudos
6 Replies
JoshuaBixby
MVP Esteemed Contributor

The code as written is using NEWFIELD for the input values, is that what you want?  You said you want to "populate a new field with just the first two words from another field," which makes me think NEWFIELD is the field you want to update and not the field you want to get values from.

Field names aside, there are a couple of risks with your code:  1) a SQL NULL that gets converted to Python None will generate AttributeError: 'NoneType' object has no attribute 'split', and 2)  a field with only 1 word in it will not have items in the list so retrieving the second item will generate IndexError: list index out of range.    I suspect it is that latter issue you are running into.

Are you sure you don't have any SQL NULLs in the rows you are processing?  If you might, you want to add some additional code to check for NULL or you want to pre-select the non-NULL records before running your code.

rahulkumar11
New Contributor

The AttributeError is an exception thrown when an object does not have the attribute you tried to access. 'NoneType' object has no attribute 'split' often indicates that the attribute you are trying to split is Null, meaning there is no value in it to split. So, you need to check the attribute is not Null before splitting. Something like..

if val is not None:
# ...

 

0 Kudos
JodieRobertson
New Contributor

Yes, you are right, the code I had actually been using was !Name!.split(" ")[0] + "_" + !Name!.split(" ")[1], not NEWFIELD, that was a mistake in the question.

There was one name with only one word that I had missed, therefore it would have created a NULL value. Is there a way to write something like an if/else statement to ignore null values, so I just get one word where only one exists and two where two exist?

0 Kudos
DanPatterson
MVP Esteemed Contributor

If your intent is to get the last name (aka second), then you can do this

# ---- demo
name = ['a', 'a b', 'a b c']

[i.split(" ")[-1] for i in name]

['a', 'b', 'c']‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

But if you want to concatenate them, then you can do this

[" ".join(i.split(" ")[:2]) for i in name]

['a', 'a b', 'a b']

So the field calculator expression is one of the above like

" ".join(!name!.split(" ")[:2])

... sort of retired...
JoshuaBixby
MVP Esteemed Contributor

Try:

!Name! if !Name! is None else "_".join(!Name!.split(" ")[:2])‍‍
0 Kudos
JoeBorgione
MVP Emeritus

 Is there a way to write something like an if/else statement to ignore null values, so I just get one word where only one exists and two where two exist?

Index out of range errors are common and the len() method should come in handy for you.  Try something like this:

if len(!Field!.split(' ')) == 1:
    #process as one word
else:
    #process as multiple words‍‍‍‍
That should just about do it....
0 Kudos