Search code examples
pythonpandassplitindex-error

'IndexError: list index out of range' when applying lambda function to a column in pandas


I have a pandas dataframe in which one of the columns contains address information and I want to slice the address to only provide the zipcode and put this into a new column. For example a typical address looks like this:

609 Lizeth Streets Bolton MA 01740 US.

To grab the zip I tried:

split_zip = lambda x: str(x).split()[-2]
df['Zipcode'] = df['Address'].apply(split_zip)

Doing that I get an

'IndexError: list index out of range'

Sidenote: When I don't specify an index, it will put the split list in the column just as I would expect (i.e. [609, Lizeth, Streets, Bolton, MA, 01740, US]). I can see that the zip is in the [-2] position and I just don't know why it won't grab it. Additionally, trying to grab the [1] index also throws the same error. The only index that seems to work is when I use [-1] which grabs 'US'

I'm fairly new to python and working with data in pandas so any help would be much appreciated!


Solution

  • Here's a way you can try:

    df['Zipcode'] = df['Address'].str.split(' ').str[-2]