In this post, we will see how to convert Numpy arrays to Pandas DataFrame.
Table of Contents
data
, index
, columns
and dtype
as parameters.
1 2 3 |
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.
1 2 3 4 5 6 7 8 |
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:
1 2 3 4 5 6 7 8 9 |
-------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.
1 2 3 4 5 6 7 8 |
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:
1 2 3 4 5 6 7 8 9 |
-------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:
1 2 3 4 5 6 7 8 9 |
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:
1 2 3 4 5 6 7 8 9 |
-------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:
1 2 3 4 5 6 7 8 |
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:
1 2 3 4 5 6 |
-------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