6

I have a Pandas DataFrame column with multiple lists within a list. Something like this:

df
     col1
0    [[1,2], [2,3]]
1    [[a,b], [4,5], [x,y]] 
2    [[6,7]]

I want to split the list over multiple columns so the output should be something like:

    col1    col2     col3
0   [1,2]   [2,3]   
1   [a,b]   [4,5]    [x,y]
2   [6,7]

Please help me with this. Thanks in advance

jpp
  • 159,742
  • 34
  • 281
  • 339
Ronnie
  • 391
  • 2
  • 6
  • 19

2 Answers2

7

You can use pd.Series.apply:

df = pd.DataFrame({'col1': [[[1, 2], [2, 3]],
                            [['a', 'b'], [4, 5], ['x', 'y']],
                            [[6, 7]]]})

res = df['col1'].apply(pd.Series)

print(res)

        0       1       2
0  [1, 2]  [2, 3]     NaN
1  [a, b]  [4, 5]  [x, y]
2  [6, 7]     NaN     NaN
jpp
  • 159,742
  • 34
  • 281
  • 339
5

I think need DataFrame contructor if performance is important:

df = pd.DataFrame(df['col1'].values.tolist())
print (df)
        0       1       2
0  [1, 2]  [2, 3]    None
1  [a, b]  [4, 5]  [x, y]
2  [6, 7]    None    None

If need remove NaNs - missing values first add dropna:

df = pd.DataFrame(df['col1'].dropna().values.tolist())
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252