3 Ways to Convert a NumPy Array to Pandas DataFrame

A NumPy array can be converted to a Pandas DataFrame through a variety of methods, including passing a NumPy array to pd.DataFrame and using the from_records() method. Learn more.

Written by Dario Radečić
Published on Feb. 18, 2025
Developer coding in python
Image: Shutterstock / Built In
Brand Studio Logo

If you want to convert a NumPy array to Pandas DataFrame, you have three options. The first two boil down to passing in a one-dimensional or two-dimensional NumPy array to a call to pd.DataFrame, and the last one leverages the built-in from_records() method. You’ll learn all three approaches today, with a ton of hands-on examples.

How to Convert a NumPy Array to Pandas DataFrame

The following code snippet converts a NumPy array to Pandas DataFrame with a column name:

arr = np.array([1, 2, 3])

data = pd.DataFrame(arr)
data

While there are many more ways to convert a NumPy array to DataFrame, you only need these three. Everything else is just a modification and brings no novelty to the table.

Regarding library imports, you’ll need both NumPy and Pandas today, so stick these two lines at the top of your Python script or notebook:

import numpy as np
import pandas as pd

 

1. Convert One and Two-Dimensional NumPy Arrays to Pandas DataFrame 

Think of one-dimensional arrays as vectors or distinct features in the data set. For example, a one-dimensional array can represent age, first name, date of birth or job title, but it can’t represent all of them. You’d need four one-dimensional arrays to do so.

The following code snippet converts a one-dimensional NumPy array to Pandas DataFrame:

arr = np.array([1, 2, 3])

data = pd.DataFrame(arr)
data

It’s just a vector of numbers, so the resulting DataFrame won’t be too interesting:

DataFrame from NumPy array
DataFrame from NumPy array. | Image: Dario Radečić

In case you want to convert a NumPy array to Pandas DataFrame with a column name, you’ll have to provide a value to the columns argument. It has to be a list, so keep that in mind:

arr = np.array([1, 2, 3])

data = pd.DataFrame(arr, columns=["Number"])
data

The resulting DataFrame has a bit more context now:

DataFrame from NumPy array with column name
DataFrame from NumPy array with column name. | Image: Dario Radečić

Now, DataFrames with only a single feature aren’t the most interesting, so let’s see how we can spice things up with multidimensional NumPy arrays.

More on Pandas6 Ways to Convert a List to Pandas DataFrame

Two-Dimensional NumPy Array to Pandas DataFrame

Think of two-dimensional arrays as matrices. We have rows and columns, where each row represents the values for one observation, measured across multiple features (columns). Each column contains information on the same feature across multiple observations.

Let’s go through a dummy example first, just so you can grasp how to leverage Pandas to create DataFrame from an array:

arr = np.array([
 [1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]
])

data = pd.DataFrame(arr, columns=["Num 1", "Num 2", "Num 3"])
data

The DataFrame has three observations (rows) measured through three features (columns):

DataFrame from Multidimensional NumPy array
DataFrame from Multidimensional NumPy array. | Image: Dario Radečić

Maybe the dummy example doesn’t paint you the full picture, so take a look at the following example. In it, we’re declaring a two-dimensional NumPy array of employees.

Each row is a single observation showcasing details of each employee’s first name, last name and email address. Each column is essentially a one-dimensional array (vector) representing either first names, last names or emails, across all records:

employees = np.array([
 ["Bob", "Doe", "[email protected]"],
 ["Mark", "Markson", "[email protected]"],
 ["Jane", "Swift", "[email protected]"],
 ["Patrick", "Johnson", "[email protected]"]
])

data = pd.DataFrame(employees, columns=["First name", "Last name", "Email"])

Here’s the resulting DataFrame:

DataFrame from real data in NumPy arrays
DataFrame from real data in NumPy arrays. | Image: Dario Radečić

And that’s how you can convert both one–dimensional and two-dimensional NumPy arrays to Pandas DataFrames. Let’s take a look at another way of doing the same thing, which is with the built-in from_records() method.

 

2. Convert NumPy Array to Pandas DataFrame With the from_records() Method

Pandas has a built-in method that allows you to convert a multidimensional NumPy array to Pandas DataFrame. It’s called from_records(), and it’s specific to the DataFrame class.

Truth be told, you don’t have to use it, since it provides no advantage over the conversion approaches we’ve covered so far. But still, if you want a dedicated method, here’s how to use it:

employees = np.array([
    ["Bob", "Doe", "[email protected]"],
    ["Mark", "Markson", "[email protected]"],
    ["Jane", "Swift", "[email protected]"],
    ["Patrick", "Johnson", "[email protected]"]
])

data = pd.DataFrame.from_records(employees, columns=["First name", "Last name", "Email"])
data

The resulting Pandas DataFrame is identical to the one from the previous section:

DataFrame from NumPy array with from_records()
DataFrame from NumPy array with from_records(). | Image: Dario Radečić

 

3. Convert NumPy Array to DataFrame Column

You can use NumPy to add additional columns to an existing Pandas DataFrame. For example, the following code snippet declares a Pandas DataFrame from a two-dimensional NumPy array:

employees = np.array([
 ["Bob", "Doe", "[email protected]"],
 ["Mark", "Markson", "[email protected]"],
 ["Jane", "Swift", "[email protected]"],
 ["Patrick", "Johnson", "[email protected]"]
])

data = pd.DataFrame(employees, columns=["First name", "Last name", "Email"])
data

 

Image 6 - DataFrame from 2D NumPy array (Image by author)

To convert a NumPy array to a DataFrame column, you only have to declare a new NumPy array and assign it to a new column. Here’s the code:

years_of_experience = np.array([5, 3, 8, 12])

data["Years of Experience"] = years_of_experience
data

The DataFrame now has four columns instead of three:

Adding a DataFrame column from NumPy array
Adding a DataFrame column from NumPy array. | Image: Dario Radečić

And that’s all for today. Let’s make a short recap next.

A tutorial on how to convert a NumPy array to Pandas DataFrame. | Video: Statistics Globe

More on PandasPandas Groupby: 5 Methods to Know

 

Understanding How to Convert NumPy Array to Pandas DataFrame

To conclude, Python’s Pandas library provides a user-friendly API for converting most common data types into Pandas DataFrames, with NumPy array being one of them. This article covered three ways to convert a NumPy array to Pandas DataFrame.

There are some variations to these approaches, but they have nothing to do with Pandas. Learn these three, and you’ll be ready for any data analysis project coming your way.

Frequently Asked Questions

Yes, Pandas can work with NumPy arrays, just as well as with plain Python lists. You can declare either a bunch of 1D arrays or a single 2D NumPy array and convert it to a Pandas DataFrame by passing it into the pd.DataFrame() call. Just remember to specify the column names, otherwise, the default range index will be used.

You can use either a call to pd.DataFrame() or the pd.DataFrame.from_records() method. Both of these work identically, and you can leverage them to convert a 2D NumPy array (matrix) to a Pandas DataFrame.

You can create a NumPy array from a plain Python list, or even multiple lists. If lists have equal lengths, NumPy will convert them into a multi-dimensional array of numbers. If they have different lengths, NumPy will default to `dtype=object` construction, meaning the NumPy array will contain separate list objects.

Explore Job Matches.