How to convert pandas DataFrame to NumPy array?

Pandas to NumPy Conversion: In this tutorial, we will learn how can we convert a pandas DataFrame to NumPy array with the help of example? By Pranit Sharma Last updated : April 18, 2023


In python pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and the data. DataFrame can be created with the help of python dictionaries. Pandas also allow us to convert this DataFrame into a NumPy array using DataFrame.to_numpy() method.

Pandas DataFrame to NumPy Array Conversion

To convert a Panda DataFrame to NumPy array, simply use the DataFrame.to_numpy() method which is called with the DataFrame that we want to convert and it returns a 2D NumPy array of the same size/dimensions.



The dtype is the data type passed to the method, apart from dtype, it has two more optional parameters, i.e., copy and na_value.

Let us understand with the help of an example.

Python program to convert pandas DataFrame to NumPy array

# Importing pandas package
import pandas as pd

# Creating a dictionary of student marks
d = {

# Now, Create DataFrame
df = pd.DataFrame(d)

# Printing the DataFrame

# Converting the DataFrame into NumPy array

# Printing the array
print("Converted NumPy array:\n",arr)


Convert pandas DataFrame to NumPy array

Python Pandas Programs »

All Python Pandas Programs

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.