Convert Pandas Dataframe To NumPy Array

This article will discuss how to convert Pandas Dataframe to Numpy Array.

Table of Contents

A Dataframe is a data structure that stores the data in rows and columns. We can create a DataFrame using pandas.DataFrame() method. Numpy Array is a sequential data structure for scientific computation in Python. Let’s discuss the methods which convert Pandas Dataframe to Numpy Array.

Convert Dataframe to NumPy Array using to_numpy()

Dataframe provides a member function to_numpy(), which can be used to convert the DataFrame to Numpy Array.

Syntax is as follows,

dataframe.to_numpy(dtype,copy,na_value)

where,

Advertisements
  • dataframe is the input pandas dataframe.
  • dtype is an optional parameter that is used to specify the type of array after converting to Array.
  • copy is an optional parameter that is used to return an new Array if specified True.
  • na_value is an optional parameter is used to specify value where null values are present.

Let’s see some examples.

Before that We create the DataFrame. Here, we are going to create the DataFrame named data with 4 rows and 3 columns.

#import pandas module
import pandas as pd

#create the dataframe with 3 columns
data=pd.DataFrame({'id':[7058,7069,7060,7061],
                   'age':[21,23,22,21],
                   'cgpa':[9.8,9.0,8.0,9.6]})

#display
print(data)

Output:

     id  age  cgpa
0  7058   21   9.8
1  7069   23   9.0
2  7060   22   8.0
3  7061   21   9.6

Convert One DataFrame column to Numpy Array

We can convert a single column from Dataframe to a Numpy Array. For that we have to specify the column name to convert DataFrame column to Numpy Array.

Syntax:

dataframe['column_name'].to_numpy(dtype,copy,na_value)

Here we are converting age and cgpa columns in pandas dataframe to numpy array individually with different types.

#convert age column to numpy array to float type
print(data['age'].to_numpy('float'))

#convert age column to numpy array to integer type
print(data['age'].to_numpy('int'))

#convert cgpa column to numpy array to float type
print(data['cgpa'].to_numpy('float'))

#convert cgpa column to numpy array to integer type
print(data['cgpa'].to_numpy('int'))

Output:

[21. 23. 22. 21.]
[21 23 22 21]
[9.8 9.  8.  9.6]
[9 9 8 9]

It return the Dataframe column as a numpy array.

Convert entire DataFrame to Numpy Arrays

Syntax:

dataframe.to_numpy(dtype,copy,na_value)

Here we are converting pandas dataframe to numpy array with different types.

#convert all columns to numpy array to float type
print(data.to_numpy('float'))

#convert all columns to numpy array to integer type
print(data.to_numpy('int'))

Output:

[[7058.    21.     9.8]
 [7069.    23.     9. ]
 [7060.    22.     8. ]
 [7061.    21.     9.6]]


[[7058   21    9]
 [7069   23    9]
 [7060   22    8]
 [7061   21    9]]

It will return the numpy array from pandas dataframe.

Convert Dataframe to NumPy Array using Dataframe.values

We can use the values attribute of Dataframe to convert it to Numpy Array.

Syntax:

dataframe.values

where,

  • dataframe is the input pandas dataframe.
  • values is the method that will convert entire dataframe into numpy array

Convert One DataFrame column to Numpy Array

We have to specify the column name to convert DataFrame column to Numpy Array.

Syntax:

dataframe['column_name'].values

Here we are converting id and age columns in pandas dataframe to numpy array individually.

#convert cgpa columns to numpy array
print(data['id'].values)

#get the type
print(type(data.values))

#convert age columns to numpy array
print(data['age'].values)

#get the type
print(type(data.values))

Output:

[7058 7069 7060 7061]
<class 'numpy.ndarray'>
[21 23 22 21]
<class 'numpy.ndarray'>

It returned the numpy array from pandas dataframe and we are also displayed the class of the returned Numpy Array using type()
function.

Convert entire DataFrame to Numpy Array

Syntax:

dataframe.values

Here we are converting pandas dataframe to numpy array.

#convert all columns to numpy array
print(data.values)

#get the type
print(type(data.values))

Output:

[[7058.    21.     9.8]
 [7069.    23.     9. ]
 [7060.    22.     8. ]
 [7061.    21.     9.6]]

<class 'numpy.ndarray'>

It will return the numpy array from pandas dataframe

Convert Dataframe to Numpy Array using to_records()

This method is used to convert the DataFrame to Numpy record Array

Syntax:

dataframe.to_records(index)

where,

  • dataframe is the input pandas dataframe.
  • index is an optional parameter used to specify the index value to each row of the numpy array created from the pandas dataframe

index = True – return the index.
index = False – will not return the index.

Example:

Here we are converting dataframe into numpy array using to_records method.

#convert id  columns to numpy array with out index
print(data.to_records(index=False))

#get the type
print(type(data.to_records()))

#convert id  columns to numpy array with  index
print(data.to_records(index=True))

#get the type
print(type(data.to_records()))

Output:

[(7058, 21, 9.8) (7069, 23, 9. ) (7060, 22, 8. ) (7061, 21, 9.6)]
<class 'numpy.recarray'>

[(0, 7058, 21, 9.8) (1, 7069, 23, 9. ) (2, 7060, 22, 8. )
 (3, 7061, 21, 9.6)]
<class 'numpy.recarray'>

It will return the list of tuple , such that tuple specifies the numpy array values, we are converting into numpy array with and with out index.

Summary

In this article we discussed three methods for converting the Pandas DataFrame into Numpy Array with examples.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top