Convert numpy array to Pandas DataFrame

In this post, we will see how to convert Numpy arrays to Pandas DataFrame.
[toc]
You can use DataFrame’s contructor to create Pandas DataFrame from Numpy Arrays. This constructor takes data, index, columns and dtype as parameters.

pd.DataFrame(data, index, columns, dtype)

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.

import numpy as np
import pandas as pd
nArray = np.arange(6).reshape(2,3)
print("-------Original ndarray-------\n",nArray)
df = pd.DataFrame(nArray)
print("-------Converted DataFrame-------\n",df)

Output:

-------Original ndarray-------
[[0 1 2]
[3 4 5]]
-------Converted DataFrame-------
0  1  2
0  0  1  2
1  3  4  5

Create DataFrame with data, index and columns

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

import numpy as np
import pandas as pd
nArray = np.arange(6).reshape(2,3)
print("-------Original ndarray-------\n",nArray)
df = pd.DataFrame(nArray,index= ['One','Two'], columns=['C1','C2','C3'])
print("-------Converted DataFrame-------\n",df)

Output:

-------Original ndarray-------
[[0 1 2]
[3 4 5]]
-------Converted DataFrame-------
C1  C2  C3
One   0   1   2
Two   3   4   5

Alter DataFrame columns after it is created

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

import numpy as np
import pandas as pd
nArray = np.arange(6).reshape(2,3)
print("-------Original ndarray-------\n",nArray)
df = pd.DataFrame(nArray,index= ['One','Two'], columns=['C1','C2','C3'])
df.columns=['Col1','Col2','Col3']
print("-------Converted DataFrame-------\n",df)

Output:

-------Original ndarray-------
[[0 1 2]
[3 4 5]]
-------Converted DataFrame-------
Col1  Col2  Col3
One     0     1     2
Two     3     4     5

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:

import numpy as np
import pandas as pd
df = pd.DataFrame({'a':[10,20],'b':[30,40]})
nArray = np.arange(6).reshape(2,3)
df = pd.concat([df, pd.DataFrame(nArray)], axis=1)
print("-------Converted DataFrame-------\n",df)

Output:

-------Converted DataFrame-------
a   b  0  1  2
0  10  30  0  1  2
1  20  40  3  4  5

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

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.