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

Can you separate a list of strings in a Pandas column to individual strings per row

I have checked online and on Stack Overflow and can’t seem to get an answer for this. Assume I have a dataframe that looks as such:

   colA          colG
0     1  [a, b, c, d]
1     2        [c, d]
2     3        [e, f]
3     4        [g, h]
4     5        [i, j]

Is it possible to flatten colG such that I get each letter assigned to a new row, something like:

   colA          colG    colH
0     1  [a, b, c, d]      a
1     1  [a, b, c, d]      b
2     1  [a, b, c, d]      c
3     1  [a, b, c, d]      d
4     2  [c, d]            c
5     2  [c, d]            d

NB: The values for other columns other than the index remain consistent, with the aim of dropping the list of str column (colG) after the operation

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 can do that using df.explode('colG')

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