Open In App

Pandas Dataframe.to_numpy() – Convert dataframe to Numpy array

Last Updated : 21 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

to_numpy() method converts Pandas DataFrame into a NumPy array. This method is used when we need to perform numerical operations that require a NumPy array instead of a DataFrame structure. In this article, we will see how to convert a dataframe to anumpy array.

Syntax: Dataframe.to_numpy(dtype = None, copy = False) 

Parameters: 

  • dtype: Data type which we are passing like str. 
  • copy: [bool, default False] ensures that a new array is created otherwise, it might return a view of the original data.

Returns numpy.ndarray: NumPy array representation of the DataFrame.

Example 1: Convert Full DataFrame to NumPy Array

  • df.to_numpy(): Converts a Pandas DataFrame into a NumPy array containing same data without index or column labels.
Python
import pandas as pd
df = pd.DataFrame(
	[[1, 2, 3],
	[4, 5, 6],
	[7, 8, 9],
	[10, 11, 12]],
	columns=['a', 'b', 'c'])
arr = df.to_numpy()
print('\nNumpy Array\n----------\n', arr)
print(type(arr))

Output:

to_np1

Full DataFrame to NumPy Array

Example 2: Convert Specific Columns to NumPy Array

  • arr = df[[‘a’, ‘c’]].to_numpy(): Converts selected columns ‘a’ and ‘c’ from DataFrame df into a NumPy array which is only containing data from those two columns.
Python
import pandas as pd
df = pd.DataFrame(
	[[1, 2, 3],
	[4, 5, 6],
	[7, 8, 9],
	[10, 11, 12]],
	columns=['a', 'b', 'c'])
arr = df[['a', 'c']].to_numpy()
print('\nNumpy Array\n----------\n', arr)
print(type(arr))

Output:

to_np2

Specific Columns to NumPy Array

Example 3: Convert DataFrame with Mixed Data Types

  • arr = df.to_numpy(): Converts entire DataFrame df into a NumPy array which is containing all the data from DataFrame without index and column labels.
Python
import pandas as pd
import numpy as np
df = pd.DataFrame(
	[[1, 2, 3],
	[4, 5, 6.5],
	[7, 8.5, 9],
	[10, 11, 12]],
	columns=['a', 'b', 'c'])
arr = df.to_numpy()
print('Numpy Array', arr)
print('Numpy Array Datatype :', arr.dtype)

Output:

to_np3

DataFrame with Mixed Data Types

Example 4: Convert DataFrame Using CSV File

We are using a CSV file for changing Dataframe into a Numpy array.You can download the dataset from here-nba.csv.

  • data.dropna(inplace=True): Removes any rows with missing values (NaN) from the DataFrame data and updates DataFrame in place.
  • df = pd.DataFrame(data[‘Weight’].head()): Creates a new DataFrame df containing only first 5 values from the ‘Weight’ column of data. The head() function selects first 5 rows by default.
Python
import pandas as pd
data = pd.read_csv("/content/nba.csv")
data.dropna(inplace=True)
df = pd.DataFrame(data['Weight'].head())
print(df.to_numpy())

Output:

to_np4

DataFrame Using CSV File

Example 5: Convert with Specified Data Type

 In this example, we are just providing the parameters in the same code to provide dtype here.

  • df.to_numpy(dtype=’float32′): Converts DataFrame df into a NumPy array with data type explicitly set to float32 which ensures values in the resulting array are stored as 32-bit floating-point numbers.
Python
import pandas as pd
data = pd.read_csv("/content/nba.csv")
data.dropna(inplace=True)
df = pd.DataFrame(data['Weight'].head())
print(df.to_numpy(dtype='float32'))

Output:

to_np5

Specified Data Type

Example 6: Validate Type of Array After Conversion

  • type(df.to_numpy()): Returns type of the object resulting from the to_numpy() method which is a <class ‘numpy.ndarray’> indicating that DataFrame df has been converted into a NumPy array.
Python
import pandas as pd
data = pd.read_csv("/content/nba.csv")
data.dropna(inplace=True)
df = pd.DataFrame(data['Weight'].head())
print(type(df.to_numpy()))

Output:

<class ‘numpy.ndarray’>

With df.to_numpy() we can easily transform a Pandas DataFrame into a NumPy array for efficient data manipulation and numerical operations. This method ensures integration with other libraries that are dependent on NumPy arrays for computations.



Next Article
Practice Tags :

Similar Reads