6

To create a pandas dataframe from numpy I can use :

columns = ['1','2'] data = np.array([[1,2] , [1,5] , [2,3]]) df_1 = pd.DataFrame(data,columns=columns) df_1 

If I instead use :

columns = ['1','2'] data = np.array([[1,2,2] , [1,5,3]]) df_1 = pd.DataFrame(data,columns=columns) df_1 

Where each array is a column of data. But this throws error :

ValueError: Wrong number of items passed 3, placement implies 2 

Is there support in pandas in this data format or must I use the format in example 1 ?

1
  • The answer is quite simple. Numpy stores arrays as row-vectors(!) and in the erroneous example you are passing two rows with 3 values and you only have 2 columns. And in the first example you are passing 3 rows with 2 values with 2 columns which works!
    – Anton vBR
    CommentedMay 24, 2018 at 21:29

3 Answers 3

7

You need to transpose your numpy array:

df_1 = pd.DataFrame(data.T, columns=columns) 

To see why this is necessary, consider the shape of your array:

print(data.shape) (2, 3) 

The second number in the shape tuple, or the number of columns in the array, must be equal to the number of columns in your dataframe.

When we transpose the array, the data and shape of the array are transposed, enabling it to be a passed into a dataframe with two columns:

print(data.T.shape) (3, 2) print(data.T) [[1 1] [2 5] [2 3]] 
    2

    In the second case, you can use:

    df_1 = pd.DataFrame(dict(zip(columns, data))) 
      2

      DataFrames are inherently created in that order from an array.

      Either way, you need to transpose something.

      One option would be to specify the index=columns then transpose the whole thing. This will get you the same output.

       columns = ['1','2'] data = np.array([[1,2,2] , [1,5,3]]) df_1 = pd.DataFrame(data, index=columns).T df_1 

      Passing in data.T as mentioned above is also perfectly acceptable (assuming the data is an ndarray type).

        Start asking to get answers

        Find the answer to your question by asking.

        Ask question

        Explore related questions

        See similar questions with these tags.