split a list in an cell to row with python

I have this table:

product date value
Prdct_1Prdct_2Prdct_3Prdct_4Prdct_5Prdct_6 23.7.2022 100

and the result should be

product date value
Prdct_1 23.7.2022 100
Prdct_2 23.7.2022 100
Prdct_3 23.7.2022 100
Prdct_4 23.7.2022 100
Prdct_5 23.7.2022 100
Prdct_6 23.7.2022 100

How should I solve it with python?

>Solution :

You need to split and explode, the subtlety is how to determine the splitting points?

Assuming you want to split after the digits, use the (?<=\d)(?=\D) regex (split after a digit and before a non-digit):

out = (df.assign(product=df['product'].str.split(r'(?<=\d)(?=\D)'))
         .explode('product')
      )

Output:

   product       date  value
0  Prdct_1  23.7.2022    100
0  Prdct_2  23.7.2022    100
0  Prdct_3  23.7.2022    100
0  Prdct_4  23.7.2022    100
0  Prdct_5  23.7.2022    100
0  Prdct_6  23.7.2022    100

regex demo

Leave a Reply