0

I have a dataframe, and I want to create 5 columns from a list in one of the column

Example :

df :

ID             Data
1               ['A1','A2','A3','A4','A5']
2               ['A2','A3','A4','A5']

Note that here few list are having less than 5 columns , for those columns insert NAN in that place.

Output df:
ID     Col1     Col2    Col3    Col4    Col5     Data 
1      A1        A2      A3      A4      A5      ['A1','A2','A3','A4','A5']
2      A2        A3      A4      A5      Nan     ['A2','A3','A4','A5']
Kshitij Agrawal
  • 233
  • 1
  • 8

1 Answers1

1
df = pd.DataFrame({
    'ID': [1, 2],
    'Data': [['A1','A2','A3','A4','A5'], ['A2','A3','A4','A5']]
})

df.join(df['Data'].apply(pd.Series))

output:

    ID  Data                            0   1   2   3   4
0   1   ['A1', 'A2', 'A3', 'A4', 'A5']  A1  A2  A3  A4  A5
1   2   ['A2', 'A3', 'A4', 'A5']        A2  A3  A4  A5  NaN 

help-ukraine-now
  • 3,850
  • 4
  • 19
  • 36