Convert numpy array to Pandas DataFrame

In this post, we will see how to convert Numpy arrays to Pandas DataFrame.

You can use DataFrame’s contructor to create Pandas DataFrame from Numpy Arrays. This constructor takes data, index, columns and dtype as parameters.

Create DataFrame with Numpy array

If you don’t pass any other arguments apart from data, you will get DataFrame of ndarray type,so this is how you can convert numpy.ndarray to dataframe.

Output:

Create DataFrame with data, index and columns

You can specify index, column and dtype as well to convert numpy.ndarray to dataframe.

Output:

Alter DataFrame columns after it is created

You can alter DataFrame columns after it is created. Here is an example:

Output:

Add numpy array as new columns for pandas dataframe

In case, you want to add numpy array as new columns for pandas dataframe, you can use pd.concat() method as below:

Output:

As you can see, 3 news columns are added to existing DataFrame.
That’s all about how to convert Numpy arrays to Pandas DataFrame

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *