2

I got pandas train df which looks like this:

                 image
1  [[0, 0, 0], [1, 0, 1], [0, 1, 1]]
2  [[1, 1, 1], [0, 0, 1], [0, 0, 1]]
2  [[0, 0, 1], [0, 1, 1], [1, 1, 1]]

Is there any way to "explode" it but into columns

   1  2  3  4  5  6  7  8  9
1  0, 0, 0, 1, 0, 1, 0, 1, 1
2  1, 1, 1, 0, 0, 1, 0, 0, 1
2  0, 0, 1, 0, 1, 1, 1, 1, 1
OverFitter
  • 49
  • 8
  • Not quite straightforward from the dup link since OP has list of list in each cell. Here's a one-liner that works in this case `pd.DataFrame(df.image.apply(np.concatenate).values.tolist())` – Quang Hoang Aug 13 '20 at 00:51
  • You could use `itertools` : ``from itertools import chain; pd.DataFrame(chain.from_iterable(entry) for entry in df.image)`` – sammywemmy Aug 13 '20 at 00:55
  • Reopened @QuangHoang – Erfan Aug 13 '20 at 01:04

1 Answers1

3

np.vstack the Series of lists of lists, then reshape

pd.DataFrame(np.vstack(df['image']).reshape(len(df), -1))

   0  1  2  3  4  5  6  7  8
0  0  0  0  1  0  1  0  1  1
1  1  1  1  0  0  1  0  0  1
2  0  0  1  0  1  1  1  1  1
ALollz
  • 57,915
  • 7
  • 66
  • 89