Home > Net >  pandas explode multi column
pandas explode multi column

Time:03-28

I must use pandas 1.2.5 which supports explode() only on 1 column. The dataframe has several columns where each can have a single value or a list. In one row, several columns can have lists but it is guaranteed that all the lists in that row are the same length. What is the best way to make the dataframe explode?

Example of what I mean is to make this dataframe:

a b
1 1
20 [10,20,30]
[100,200,300] [100,200,300]

Look like this dataframe:

a b
1 1
20 10
20 20
20 30
100 100
200 200
300 300

CodePudding user response:

Since you are using old pandas version and your column does not have matching element counts therefore multi column explode is not an available option. Here is one approach which involving reshaping the dataframe into a series in order to use the single column explode, then creating a new index using groupby cumcount and reshaping back to dataframe

s = df.stack().explode()
i = s.groupby(level=[0, 1]).cumcount()
s.to_frame().set_index(i, append=True)[0].unstack(1).ffill().droplevel(1)

Result

     a    b
0    1    1
1   20   10
1   20   20
1   20   30
2  100  100
2  200  200
2  300  300
  • Related