Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading