Hack: Columns From Lists Inside A Column in Pandas

[This article was first published on Python – Predictive Hacks, and kindly contributed to python-bloggers]. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.
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 

To leave a comment for the author, please follow the link and comment on their blog: Python – Predictive Hacks.

Want to share your content on python-bloggers? click here.