Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert two numpy array to dataframe

I want to convert two numpy array to one DataFrame containing two columns. The first numpy array 'images' is of shape 102, 1024. The second numpy array 'label' is of shape (1020, )

My core code is:

images=np.array(images)
label=np.array(label)
l=np.array([images,label])
dataset=pd.DataFrame(l)

But it turns out to be an error saying that:

ValueError: could not broadcast input array from shape (1020,1024) into shape (1020)

What should I do to convert these two numpy array into two columns in one dataframe?

like image 449
Y.Z Avatar asked Sep 23 '17 11:09

Y.Z


People also ask

How do I create a DataFrame from two NumPy arrays?

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']) . Remember, that each column in your NumPy array needs to be named with columns.

How do you combine arrays into data frames?

We can use np. column_stack() to combine two 1-D arrays X and Y into a 2-D array. Then, we can use pd. DataFrame to change it into a dataframe.

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.

How do you combine two arrays in Python?

You can use the numpy. concatenate() function to concat, merge, or join a sequence of two or multiple arrays into a single NumPy array. Concatenation refers to putting the contents of two or more arrays in a single array.


1 Answers

You can't stack them easily, especially if you want them as different columns, because you can't insert a 2D array in one column of a DataFrame, so you need to convert it to something else, for example a list.

So something like this would work:

import pandas as pd
import numpy as np
images = np.array(images)
label = np.array(label)
dataset = pd.DataFrame({'label': label, 'images': list(images)}, columns=['label', 'images'])

This will create a DataFrame with 1020 rows and 2 columns, where each item in the second column contains 1D arrays of length 1024.

like image 184
MSeifert Avatar answered Oct 07 '22 02:10

MSeifert