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
  • 147,904
  • 31
  • 244
  • 302
Ronnie
  • 381
  • 1
  • 6
  • 17

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
  • 147,904
  • 31
  • 244
  • 302
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
  • 729,927
  • 78
  • 1,141
  • 1,090