2

I have my pandas df like below. It has list in one of the column can it be unwind as below:?

import pandas as pd
L1 = [['ID1', 0, [0, 1, 1] , [0, 1]],
      ['ID2', 2, [1, 2, 3], [0, 1]]
      ]

df1 = pd.DataFrame(L1,columns=['ID', 't', 'Key','Value'])

can this be unwinded like below?

import pandas as pd
L1 = [['ID1', 0, 0, 1, 1 , 0, 1],
      ['ID2', 2, 1, 2, 3, 0, 1]
      ]

df1 = pd.DataFrame(L1,columns=['ID', 't', 'Key_0','Key_1','Key_2','Value_0', 'Value_1'])
Gaurav
  • 39
  • 2
  • Does this answer your question? [Pandas split column of lists into multiple columns](https://stackoverflow.com/questions/35491274/pandas-split-column-of-lists-into-multiple-columns) – Ynjxsjmh Apr 30 '21 at 13:49
  • @Ynjxsjmh It doesn't. Based on the answers you need to specify if Key column need to be split to Key 0, Key 1 and Key 2...this is because key has 3 elements. Ideally in the different scenario when you have n elements solution should accommodate from key_0 to key_n-1....so this is different – Gaurav Apr 30 '21 at 14:10

3 Answers3

1

Turning the Series into lists you can call the DataFrame constructor to explode them into multiple columns. Using pop in a list comprehension within concat will remove the original columns from your DataFrame so that you just join back the exploded versions.

This will work regardless of the number of elements in each list, and even if the lists have varying numbers of elements across rows.

df2 = pd.concat(([df1] + [pd.DataFrame(df1.pop(col).tolist(), index=df1.index).add_prefix(f'{col}_')
                          for col in ['Key', 'Value']]), 
                axis=1)

print(df2)

    ID  t  Key_0  Key_1  Key_2  Value_0  Value_1
0  ID1  0      0      1      1        0        1
1  ID2  2      1      2      3        0        1
ALollz
  • 57,915
  • 7
  • 66
  • 89
1

You can flatten L1 before constructing the data frame:

L2 = [ row[0:2] + row[2] + row[3] for row in L1 ]
df2 = pd.DataFrame(L2,columns=['ID', 't', 'Key_0','Key_1','Key_2','Value_0', 'Value_1'])
Code Different
  • 90,614
  • 16
  • 144
  • 163
0

You can use explode the dataframe columns wise-

df3 = df1.apply(pd.Series.explode, axis=1)
df3.columns = ['ID', 't', 'Key_0','Key_1','Key_2','Value_0', 'Value_1']
Nk03
  • 14,699
  • 2
  • 8
  • 22