Predictive Hacks

Hack: Columns From Lists Inside A Column in Pandas

columns from lists in column pandas
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 

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.