I have an numpy.ndarray as given below:
x = np.array([[1, 2, 3], [4, 5, 6]], np.int32)
labels = [1,0]
df = pd.DataFrame({"a":x,"labels":labels})
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-458-79198b72cdcb> in <module>()
1 x = np.array([[1, 2, 3], [4, 5, 6]], np.int32).reshape(-1,1)
2 labels = [1,0,1,0]
----> 3 df = pd.DataFrame({"a":x,"labels":labels})
4 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/construction.py in sanitize_array(data, index, dtype, copy, raise_cast_failure)
480 elif subarr.ndim > 1:
481 if isinstance(data, np.ndarray):
--> 482 raise Exception("Data must be 1-dimensional")
483 else:
484 subarr = com.asarray_tuplesafe(data, dtype=dtype)
Exception: Data must be 1-dimensional
I tried to reshape the np.ndarray by x.reshape(-1,1) but the result didn't change. Each of the lists in ndarray x must be a row in the dataframe. I'm expecting to get:
a labels
0 [1, 2, 3] 1
1 [4, 5, 6] 0
xfrom a list of lists,xitself does not contain lists. It is a 2d array containing numbers.x=[[1, 2, 3], [4, 5, 6]]works.