How can I create a Pandas DataFrame from a NumPy array in Python?

Hello everyone, I was trying to create a dataframe from a NumPy array and I had a 2D array and used the following code:

I noticed that by using this method, I got a dataframe with rows corresponding to the inner lists in the array, are there different alternatives for doing this? And can I create column-wise data using arrays? And lastly, how would I be able to use a 1D array and create it into a dataframe?

Hi @mubashir_rizvi This may help you:

  • In this method, we have created a 2D NumPy array that contains tuples, and each tuple represents one row of the data frame. We have also passed an additional argument, of dtype in which we have specified the data type and name for each column.
  • The advantage of specifying the data type and the name is that when we use the pd.DataFrame.from_records() function, we don’t have to specify the names of the columns again and we don’t have to worry about the data type.

Hello @mubashir_rizvi , for creating 2D array column-wise, you can use the following method: