How to convert Numpy array to Panda DataFrame
You could flatten the numpy array:
import numpy as npimport pandas as pddata = [[400.31865662], [401.18514808], [404.84015554], [405.14682194], [405.67735105], [273.90969447], [274.0894528]]arr = np.array(data)df = pd.DataFrame(data=arr.flatten())print(df)
Output
00 400.3186571 401.1851482 404.8401563 405.1468224 405.6773515 273.9096946 274.089453
Since I assume the many visitors of this post aren't here for OP's specific and un-reproducible issue, here's a general answer:
df = pd.DataFrame(array)
The strength of pandas
is to be nice for the eye (like Excel), so it's important to use column names.
import numpy as npimport pandas as pdarray = np.random.rand(5, 5)
array([[0.723, 0.177, 0.659, 0.573, 0.476], [0.77 , 0.311, 0.533, 0.415, 0.552], [0.349, 0.768, 0.859, 0.273, 0.425], [0.367, 0.601, 0.875, 0.109, 0.398], [0.452, 0.836, 0.31 , 0.727, 0.303]])
columns = [f'col_{num}' for num in range(5)]index = [f'index_{num}' for num in range(5)]
Here's where the magic happens:
df = pd.DataFrame(array, columns=columns, index=index)
col_0 col_1 col_2 col_3 col_4index_0 0.722791 0.177427 0.659204 0.572826 0.476485index_1 0.770118 0.311444 0.532899 0.415371 0.551828index_2 0.348923 0.768362 0.858841 0.273221 0.424684index_3 0.366940 0.600784 0.875214 0.108818 0.397671index_4 0.451682 0.836315 0.310480 0.727409 0.302597
There is another way, which isn't mentioned in the other answers. If you have a NumPy array which is essentially a row vector (or column vector) i.e. shape like (n, )
, then you could do the following :
# sample arrayx = np.zeros((20))# empty dataframedf = pd.DataFrame()# add the array to df as a columndf['column_name'] = x
This way you can add multiple arrays as separate columns.