Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas split and select the second element

Tags:

python

pandas

I have a dataframe like this:

item_id
26--_-23
24--_-65
12
24--_-54
24
66
23

When I say

df['item_id'] = df['item_id'].map(lambda x: x.split('--_-')[0])

I get:

item_id
26
24
12
24
24
66
23

Which is alright. But when I say:

df['par_id'] = df['item_id'].map(lambda x: x.split('--_-')[1])

I get the following error:

df2['item_id'] = df2['item_id'].map(lambda x: x.split('--_-')[1])
IndexError: list index out of range

Then I decided to try:

def split(x):
    try: z = x.split('--_-')[1]
    except:  z = None
df2['par_id'] = df2['item_id'].apply(split)

But in this case, I get an empty par_id column I know, it is probably a very trivial question, but what is the problem here?

like image 501
edyvedy13 Avatar asked Dec 23 '17 12:12

edyvedy13


2 Answers

You can use pandas function str.split and select lists by positions by str - if values not exist get NaNs:

df['item_id'] = df['item_id'].str.split('--_-').str[1]
print (df)
  item_id
0      23
1      65
2     NaN
3      54
4     NaN
5     NaN
6     NaN

Detail:

print (df['item_id'].str.split('--_-'))
0    [26, 23]
1    [24, 65]
2        [12]
3    [24, 54]
4        [24]
5        [66]
6        [23]
Name: item_id, dtype: object
like image 136
jezrael Avatar answered Sep 21 '22 05:09

jezrael


split on space, slice off first item, join using commas:

df['Key'].str.split(' ').str[1:].str.join(',')
like image 40
BSalita Avatar answered Sep 19 '22 05:09

BSalita