1

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

Mwangi Kabiru
  • 423
  • 2
  • 10

1 Answers1

3

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

rafaelc
  • 57,686
  • 15
  • 58
  • 82
bpfrd
  • 945
  • 3
  • 11
  • Just gotta merge back to the original data frame, in case you want to keep the columns `colG` with the lists too. – rafaelc Jul 20 '22 at 12:40