0

I have 18x1 dataframe and all the rows of the dataframe have a an array of length 18. I want to convert the 18x1 dataframe to 18x18 dataframe by converting each array into 18 columns

I am new to python, so any help would be appreciated

As18
  • 1
  • 1

1 Answers1

0

If the size of the arrays is consistent, the simplest might be to convert to array, then back to DataFrame.

Assuming "col" your column:

import numpy as np
df2 = pd.DataFrame(np.vstack(df['col']))

# or
df2 = pd.DataFrame(df['col'].tolist())

Example input (5x5 only):

df = pd.DataFrame({'col': [np.arange(5)]*5})

               col
0  [0, 1, 2, 3, 4]
1  [0, 1, 2, 3, 4]
2  [0, 1, 2, 3, 4]
3  [0, 1, 2, 3, 4]
4  [0, 1, 2, 3, 4]

Output:

   0  1  2  3  4
0  0  1  2  3  4
1  0  1  2  3  4
2  0  1  2  3  4
3  0  1  2  3  4
4  0  1  2  3  4
mozway
  • 194,879
  • 13
  • 39
  • 75