1

How to explode the list into rows?

I have the following data frame:

df = pd.DataFrame([
        (1,
            [1,2,3],
            ['a','b','c']
        ),
        (2,
            [4,5,6],
            ['d','e','f']
        ),
        (3,
            [7,8],
            ['g','h']
        )
])

Shown in output as follows

   0          1          2
0  1  [1, 2, 3]  [a, b, c]
1  2  [4, 5, 6]  [d, e, f]
2  3     [7, 8]     [g, h]

I want to have the following output:

    0   1   2
0   1   1   a
1   1   2   b
2   1   3   c
3   2   4   d
4   2   5   e
5   2   6   f
6   3   7   g
7   3   8   h
buhtz
  • 10,774
  • 18
  • 76
  • 149
m0nhawk
  • 22,980
  • 9
  • 45
  • 73

1 Answers1

2

You can use str.len for get length of lists which are repeated by numpy.repeat with flattening lists:

from  itertools import chain
import numpy as np

df2 = pd.DataFrame({
        0: np.repeat(df.iloc[:,0].values, df.iloc[:,1].str.len()),
        1: list(chain.from_iterable(df.iloc[:,1])),
        2: list(chain.from_iterable(df.iloc[:,2]))})

print (df2)        
   0  1  2
0  1  1  a
1  1  2  b
2  1  3  c
3  2  4  d
4  2  5  e
5  2  6  f
6  3  7  g
7  3  8  h
Community
  • 1
  • 1
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252