RCarmody
RCarmody

Reputation: 720

New Column Based on Last Delimiter Split

I am getting an index error while trying to use a lambdas function like below... I am trying to extract just the last 2-3 characters from the string based on a space as a delimiter. Why would this not work?

Error:

Traceback (most recent call last):
  File "C:\Users\robert.carmody\OneDrive - Accenture\Python\Global T&O Learning.py", line 116, in <module>
    report_demand['Industry'] = report_demand['Industry'].apply(lambda x: x.rsplit(' ', 1)[1])
  File "C:\Python38\lib\site-packages\pandas\core\series.py", line 3848, in apply
    mapped = lib.map_infer(values, f, convert=convert_dtype)
  File "pandas\_libs\lib.pyx", line 2329, in pandas._libs.lib.map_infer
  File "C:\Users\robert.carmody\Global T&O Learning.py", line 116, in <lambda>
    report_demand['Industry'] = report_demand['Industry'].apply(lambda x: x.rsplit(' ', 1)[1])
IndexError: list index out of range

Code:

report_demand['Industry'] = report_demand['Industry'].astype(str)
report_demand['Industry'] = report_demand['Industry'].apply(lambda x: x.rsplit(' ', 1)[1])

example string: "Newport Chicago IL" or "Kingston Jamaica USR"

expected output: "IL" and "USR"

Upvotes: 0

Views: 25

Answers (1)

Celius Stingher
Celius Stingher

Reputation: 18367

If you are looking for the last element of the list, then you would need to use [-1] instead of [1]. Furthermore, there's no need for apply + lambda, you can use .str.split(). Try with the following:

report_demand['Industry'] = report_demand['Industry'].str.split().str[-1]

Upvotes: 1

Related Questions