0

I'm currently learning python and trying few pandas functions in Jupyter notebook. I'm trying to split the column values based on '/' delimiter using lambda function and the error 'list index out of range' threw. The column looks as below

column_name
---------------
screen1/screen2
screen2
screen3/screen4

Now, I've used the lambda functions as below:

df['new_column_name'] = df['column_name'].apply(lambda x:x.split('/')[1])

df.head()

Can someone pls help me out how can I overcome the 'list index out of range' error and split these values into a new column ?

Craig
  • 4,605
  • 1
  • 18
  • 28

1 Answers1

1

The output you expect is unclear, however you can use str.split to split the strings, and the str accessor to slice the data without worrying about IndexError.

Depending on whether you want the second or last element, you could do:

df['second'] = df['col'].str.split('/').str[1]

or

df['last']= df['col'].str.split('/').str[-1]

output:

               col   second     last
0  screen1/screen2  screen2  screen2
1          screen2      NaN  screen2
2  screen3/screen4  screen4  screen4
mozway
  • 194,879
  • 13
  • 39
  • 75
  • Thanks, it worked. I am wondering why the lambda function didn't work well – The Weighted Tooth Feb 11 '22 at 17:35
  • Because, using `apply`, you evaluate each element individually. When there is no `/` you perform `['string'][1]`, which is impossible as there is only a single element in the list. Using the `str` accessor automatically handles this case and outputs NaN if no slicing is possible. – mozway Feb 11 '22 at 18:02