Convert numpy array to Pandas DataFrame

Previous
Next

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.

Output:

Create DataFrame with data, index and columns

You can specify index, column and dtype as well.

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

Previous
Next

Add Comment