Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert list of arrays to pandas dataframe

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
like image 294
Marcos Santana Avatar asked Mar 28 '18 17:03

Marcos Santana


People also ask

How do I turn an array into a data frame?

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']) .

How can you convert a NumPy array into a pandas DataFrame?

To convert a numpy array to pandas dataframe, we use pandas. DataFrame() function of Python Pandas library.


2 Answers

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
like image 186
BENY Avatar answered Oct 17 '22 01:10

BENY


Alt 1

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

Alt 2

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
like image 32
piRSquared Avatar answered Oct 17 '22 01:10

piRSquared