I have a dataframe with two columns of lists:
>>> import pandas as pd
>>> df = pd.DataFrame({'A': ['x1','x2','x3', 'x4'], 'B':[['v1','v2'],['v3','v4'],['v6'],['v7','v8']], 'C':[['c1','c2'],['c3','c4'],['c5','c6'],['c7']]})
>>> df
A B C
0 x1 [v1, v2] [c1, c2]
1 x2 [v3, v4] [c3, c4]
2 x3 [v6] [c5, c6]
3 x4 [v7, v8] [c7]
I would like to explode columns B and C, so the output looks like this:
>>> df_exploded
A B C
0 x1 v1 c1
1 x1 v2 c2
2 x2 v3 c3
3 x2 v4 c4
4 x3 v6 c5
5 x3 v6 c6
6 x4 v7 c7
7 x4 v8 c7
My current solution is to first separate rows where elements in column B and C have the same length and run df.explode(["B", "C"]) and for the rest rows, run df.explode("B") followed by df.explode("C")
I am wondering if there's a better solution.