'How to split column values when list index goes out of range in Jupyter notebook? [duplicate]
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 ?
Solution 1:[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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mozway |
