df=pd.DataFrame({'Name':['Billy','George'],'colA':[[[3,4],[3,9],[3,0],[2,1]],[[1,2],[3,5],[1,0],[0,1]]], 'colB':[[[3,4,1,3,1],[1,2,2,2,1],[6,5,0,1,1],[1,2,1,1,1]],[[5,4,2,3,4],[5,1,2,2,5],[7,5,0,1,2],[4,2,1,1,3]]]})
Name colA colB
0 Billy [[3, 4], [2, 1]] [[3, 4, 1, 3, 1], [1, 2, 2, 2, 1]]
1 George [[1, 2], [0, 1]] [[5, 4, 2, 3, 4], [5, 1, 2, 2, 5]]
First Step: Flatten the lists
If in the columns we have list of lists we have to flatten them firtst.
df['colA']=df['colA'].apply(lambda x: [item for sublist in x for item in sublist]) df['colB']=df['colB'].apply(lambda x: [item for sublist in x for item in sublist]) df['colA']
0 [3, 4, 2, 1]
1 [1, 2, 0, 1]
Second Step: Create columns, add a prefix and drop the original
df=pd.concat([pd.DataFrame(df.colA.tolist(), index= df.index).add_prefix('colA'),df,],axis=1).drop('colA',axis=1) df=pd.concat([pd.DataFrame(df.colB.tolist(), index= df.index).add_prefix('colB'),df,],axis=1).drop('colB',axis=1)
colB0 colB1 colB2 colB3 colB4 colB5 colB6 colB7 colB8 colB9 \
0 3 4 1 3 1 1 2 2 2 1
1 5 4 2 3 4 5 1 2 2 5
colA0 colA1 colA2 colA3 Name
0 3 4 2 1 Billy
1 1 2 0 1 George