I have a list of numpy arrays that I'm trying to convert to DataFrame. Each array should be a row of the dataframe.
Using pd.DataFrame() isn't working. It always gives the error: ValueError: Must pass 2-d input.
Is there a better way to do this?
This is my current code:
list_arrays = [ array([[0, 0, 0, 1, 0, 0, 0, 0, 00]], dtype='uint8'),
array([[0, 0, 3, 2, 0, 0, 0, 0, 00]], dtype='uint8')
]
d = pd.DataFrame(list_arrays)
ValueError: Must pass 2-d input
How do you convert an array to a DataFrame in Python? To convert an array to a dataframe with Python you need to 1) have your NumPy array (e.g., np_array), and 2) use the pd. DataFrame() constructor like this: df = pd. DataFrame(np_array, columns=['Column1', 'Column2']) .
To convert a numpy array to pandas dataframe, we use pandas. DataFrame() function of Python Pandas library.
You can using pd.Series
pd.Series(l).apply(lambda x : pd.Series(x[0]))
Out[294]:
0 1 2 3 4 5 6 7 8
0 0 0 0 1 0 0 0 0 0
1 0 0 3 2 0 0 0 0 0
pd.DataFrame(sum(map(list, list_arrays), []))
0 1 2 3 4 5 6 7 8
0 0 0 0 1 0 0 0 0 0
1 0 0 3 2 0 0 0 0 0
pd.DataFrame(np.row_stack(list_arrays))
0 1 2 3 4 5 6 7 8
0 0 0 0 1 0 0 0 0 0
1 0 0 3 2 0 0 0 0 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With