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?
You can use pandas function str.split
and select lists by positions by str
- if values not exist get NaN
s:
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
split on space, slice off first item, join using commas:
df['Key'].str.split(' ').str[1:].str.join(',')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With