How to Create Pandas DataFrame from Numpy Array?
Create Pandas DataFrame from Numpy Array
To create Pandas DataFrame from Numpy Array, you can pass this array as data
argument to pandas.DataFrame().
Each row of numpy array will be transformed to a row in resulting DataFrame.
The syntax of DataFrame() class constructor is
DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)
Video Tutorial
Examples
1. Create a DataFrame from Numpy Array
In this example, we will
- Import pandas package and numpy package.
- Initialize a 2D Numpy Array
array
. - Create DataFrame by passing this numpy array
array
asdata
parameter to pandas.DataFrame(). - pandas.DataFrame(numpy array) returns DataFrame.
Python Program
import pandas as pd
import numpy as np
array = np.array([['a1', 'b1', 'c1'],
['a2', 'b2', 'c2'],
['a3', 'b3', 'c3']])
df = pd.DataFrame(array)
print(df)
Output
0 1 2
0 a1 b1 c1
1 a2 b2 c2
2 a3 b3 c3
2. Create a DataFrame from Numpy Array, along with column names and index
In this example, we will
- Import pandas package and numpy package.
- Initialize a 2D Numpy Array.
- Initialize a List for column name of DataFrame.
- Initialize a List for index of DataFrame.
- Create DataFrame by passing this numpy array object for
data
parameter to pandas.DataFrame() constructor. - pandas.DataFrame(ndarray) returns DataFrame.
Python Program
import pandas as pd
import numpy as np
array = np.array([['a1', 'b1', 'c1'],
['a2', 'b2', 'c2'],
['a3', 'b3', 'c3']])
columns = ['aN', 'bN', 'cN']
index = [1, 2, 3]
df = pd.DataFrame(array, index, columns)
print(df)
Output
aN bN cN
1 a1 b1 c1
2 a2 b2 c2
3 a3 b3 c3
Summary
In this Pandas Tutorial, we learned how to create a Pandas DataFrame from Numpy ndarray, with the help of well detailed examples.