1

I tried to flatten a small nested dataframe which is given below.

>nested dataframe

     A   B    C
0   1   1  [1, 2]
1   2   2  [2, 3]
2   3   3  [3, 4]

       

After flattening the C column I achieved this columnwise.

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

The code I used

 dat = pd.DataFrame({'A ': [1,2,3] ,
                        'B ':[1,2,3, ],
                        'C':[[1,2 ], [ 2,3],[ 3,4] ]})
 dataa= dat.explode('C') 

but what I'm expecting is to obtain something like this row wise.

    A   B  C1  C2
0   1   1  1  2
1   2   2  2  3
2   3   3  3  4

as I could easily do this in R but in python any help will be appreciated

Aparna Prasad
  • 91
  • 1
  • 6

0 Answers0