1

I have been looking at all the related examples but have not been able to adapt them to fit my example.

I have a pandas Dataframe

           0    1     2    3                     4
0         Date  Time  Col1 Col2 [Col3, Col4, Coln] 
1         Date1 Time1 1.0  2.0  [3.0,  4.0,  5.0]
2         Date2 Time2 6.0  7.0  [8.0,  9.0,  10.0]
2         Date3 Time3 11.0 12.0 [13.0, 14.0, 15.0]

And the expected output should be:

           0    1     2    3    4     5     6
0         Date  Time  Col1 Col2 Col3, Col4, Coln 
1         Date1 Time1 1.0  2.0  3.0   4.0   5.0
2         Date2 Time2 6.0  7.0  8.0   9.0   10.0
2         Date3 Time3 11.0 12.0 13.0  14.0  15.0

Note the following: The number of columns and their names are all dynamic and random. And every row will have same number of elements(ie. There will be no Missing entries).

Ian
  • 151
  • 1
  • 3
  • 9

2 Answers2

0
df[['Col3', 'Col4','Col5']] = pd.DataFrame(df.iloc[:,4].tolist(), index=df.index)   

For this simple case you can do it like this. But the overall idea would be similar in case you have many elements in list. (In that case you will generate the column names as well)

user27286
  • 486
  • 4
  • 11
0

If you do not know the length of list before hand, but know the column name in which the list is stored, then you can use

df = pd.DataFrame( {'A': [[1,2,3],['a','b','c']], 'B': [10,20] })
print (df)
print (pd.concat([df[df.columns.drop('A')], df['A'].apply(pd.Series)], axis=1))

Output:

           A   B
0  [1, 2, 3]  10
1  [a, b, c]  20

    B  0  1  2
0  10  1  2  3
1  20  a  b  c
mujjiga
  • 16,186
  • 2
  • 33
  • 51