Nitin Kumar
Nitin Kumar

Reputation: 85

Splitting a string on location using Pandas

I have a feature(Item_Identifier) that contains information about a product. It looks something like this:

0    FDA15
1    DRC01
2    FDN15
3    FDX07
4    NCD19

I am trying to split the feature into three parts, the first part should contain two characters, second to contain the third character and last should contain the numeric value. I have tried using pandas str.split() method, but I am unable to perform the split at a specific position. This is something that I have tried but I am unable to perform the split based on location.

train.Item_Identifier.str.split('',expand=True)

0    1  2   3   4   5   6
0       F   D   A   1   5   
1       D   R   C   0   1   
2       F   D   N   1   5   
3       F   D   X   0   7   
4       N   C   D   1   9   

Is there a way to perform this using str.split() method, if not what other things can I try.

Upvotes: 0

Views: 57

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

For your sample data:

df.item_identifier.str.extract('(\w{2})(\w)(\d+)')

Output:

    0  1   2
0  FD  A  15
1  DR  C  01
2  FD  N  15
3  FD  X  07
4  NC  D  19

Upvotes: 2

Related Questions