Home > Enterprise >  How to split a df column according to customized string pattern?
How to split a df column according to customized string pattern?

Time:07-07

I have a dataframe like the below:

index      value
3          05/21
6          05/23
7         330433
9     05/2430366
11         30366
12         05/25
14         05/26
15         30366
17         05/27
18         30366
20         05/28
21         30366
23         05/29
24         30366
26         05/31
27         29933

I want to split it into two volumes:

0    1       2       
1  05/21    
2  05/23   330433
3  05/24    30366
4           30366
5  05/25    
6  05/26    30366
7  05/27    30366
8  05/28    30366
9  05/29    30366
10 05/31    29933

I have tried regex, but it does not work.

import regex as re
pat = '\*d{2}\/\d{2}\d*'
s1 = '05/2130366'
re.split(pat, s1)

I need some help, thank you.

CodePudding user response:

See if the below code helps:

import regex as re
pat = '^(\d{0,2}\/\d{0,2})?'
s1 = '05/2130366'
s2 = '3456556'
s3 = '05/23'

Output:

>>> re.split(pat, s1)
['', '05/21', '30366']
>>> re.split(pat, s2)
['', None, '3456556']
>>> re.split(pat, s3)
['', '05/23', '']

Skip the first value, second & third values will be your required ones.

  • Related