pandas how to explode from two cells element-wise

Viewed 28

I have a dataframe:

df = 
A  B  C
1 [2,3] [4,5]

And I want to explode it element-wise based on [B,C] to get:

df = 
A B  C
1 2  4
1 3  5

What is the best way to do so? B and C are always at the same length.

Thanks

1 Answers

Try, in pandas 1.3.2:

df.explode(['B', 'C'])

Output:

   A  B  C
0  1  2  4
0  1  3  5
Related