1
df = pd.DataFrame({'A': ['x1','x2','x3', 'x4'], 'B': ['y1','y2','y3', 'y4'], 'To_explode_A':[['a1'],['a2'],['a3'],['a4']], 'To_explode_B':[['b1','b2','b3'],['b4','b5'],['b6','b7','b8','b9'],['b10','b11','b12']],'To_explode_C':[['c1','c2','c3','c4'],['c5','c6','c7','c8'],['c9'],['c10','c11']]})

My Input :-

    A   B   To_explode_A    To_explode_B        To_explode_C
0   x1  y1  [a1]            [b1, b2, b3]        [c1, c2, c3, c4]
1   x2  y2  [a2]            [b4, b5]            [c5, c6, c7, c8]
2   x3  y3  [a3]            [b6, b7, b8, b9]    [c9]
3   x4  y4  [a4]            [b10, b11, b12]     [c10, c11]

My Desired Output :-

    A   B   Exploded_A  Exploded_B  Exploded_C
0   x1  y1  a1          b1          c1
1   x1  y1              b2          c2
2   x1  y1              b3          c3
3   x1  y1                          c4
4   x2  y2  a2          b4          c5
5   x2  y2              b5          c6
6   x2  y2                          c7
7   x2  y2                          c8
8   x3  y3  a3          b6          c9
9   x3  y3              b7  
10  x3  y3              b8  
11  x3  y3              b9  
12  x4  y4  a4          b10         c10
13  x4  y4              b11         c11
14  x4  y4              b12 

​ I want to explode these columns in such a way that the length of the number of rows for 1 id is the maximum length of the list of any of the exploded columns and rest of values are filled with nan.

Something along the lines of :- Efficient way to unnest (explode) multiple list columns in a pandas DataFrame

but a variable list size.

0 Answers0