0
a = np.array([[5, 6, 7, 8],[5, 6, 7, 8]])

df = pd.DataFrame(a, columns=['a'])

ValueError: Shape of passed values is (2, 4), indices imply (2, 1)

I hope that the final result is:

a
-----------
[5, 6, 7, 8]
[5, 6, 7, 8] 

Edit:

    df = pd.DataFrame({"a": [a]})

    a
----------------------------------
0   [[5, 6, 7, 8], [5, 6, 7, 8]]

why?

Ivan Lee
  • 3,420
  • 4
  • 30
  • 45

2 Answers2

2

According to https://stackoverflow.com/a/18646275/5405298, you have to turn the array into a list. In your case, you can use

import pandas as pd
import numpy as np

a = np.array([[5, 6, 7, 8], [5, 6, 7, 8]])
df = pd.DataFrame({"a": a.tolist()})
print(df)

this returns:

              a
---------------
0  [5, 6, 7, 8]
1  [5, 6, 7, 8]
Leonard
  • 2,510
  • 18
  • 37
0

Convert each sub-array to string, you will have what you want.

>>>a = np.array([[5, 6, 7, 8],[5, 6, 7, 8]])
>>>df = pd.DataFrame([",".join([str(i) for i in j]) for j in a], columns=['a'])
>>>df
         a
0  5,6,7,8
1  5,6,7,8
Lê Tư Thành
  • 1,063
  • 2
  • 10
  • 19
  • if a column with list, not string? – Ivan Lee Nov 07 '19 at 04:03
  • @IvanLee, seem it doesn't work. `DataFrame` use `pandas_dtype()` to validating, and I don't see `list` in that. And in that case, `DataFrame` will detect the length of a list to require the number of columns also. – Lê Tư Thành Nov 07 '19 at 04:15