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)

Examples

1. Create a DataFrame from Numpy Array

In this example, we will

  1. Import pandas package and numpy package.
  2. Initialize a 2D Numpy Array array.
  3. Create DataFrame by passing this numpy array array as data parameter to pandas.DataFrame().
  4. 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)
Run Code Copy

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

  1. Import pandas package and numpy package.
  2. Initialize a 2D Numpy Array.
  3. Initialize a List for column name of DataFrame.
  4. Initialize a List for index of DataFrame.
  5. Create DataFrame by passing this numpy array object for data parameter to pandas.DataFrame() constructor.
  6. 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)
Run Code Copy

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.

Related Tutorials

Code copied to clipboard successfully 👍