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

Question:

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

Asked By: Mwangi Kabiru

||

Answers:

You can do that using df.explode('colG')

Answered By: bpfrd
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.