

Convert Pandas Dataframe To NumPy Array – thisPointer
source link: https://thispointer.com/convert-pandas-dataframe-to-numpy-array/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.
Advertisements
Recommend
-
3
Pandas Dataframe.loc[] – thisPointer Skip to content In this article, we will discuss how to use the loc property of the Dataframe with examples. In Pandas, t...
-
4
Pandas Dataframe.iloc[] – thisPointer Skip to content In this article, we will discuss how to use the iloc property of Dataframe with examples. In Pandas, th...
-
7
This article will discuss how to convert Numpy arrays to a Pandas DataFrame. Table of Contents A DataFrame is a data structure that will store the data in rows and columns. We can create a DataFrame using panda...
-
5
Pretty Print a Pandas Dataframe – thisPointer Skip to content In this article we will discuss how to print the a Dataframe in pretty formats.
-
9
This article will discuss how to convert JSON to pandas Dataframe. JSON stands for JavaScript Object Notation that stores the data in key-value pair format, inside the list/dictionary data structure. A DataFrame is a data structure tha...
-
9
Export Pandas Dataframe to JSON – thisPointer Skip to content In this article, we will discuss how to export a Pandas Dataframe to JSON file in Python.
-
9
Drop Duplicate Rows from Pandas Dataframe – thisPointer In this article, we will discuss different ways to delete duplicate rows in a pandas DataFrame. Table of Contents: A DataFrame is a data structure...
-
12
Convert the Column type from String to Datetime format in Pandas DataframeConvert the Column type from String to Datetime format in Pandas Dataframe10 Views30/05/2022...
-
7
In this article, we will discuss different ways to convert a Pandas Series or Index to a NumPy array in Python. Table Of Contents Overview of a Pandas Series In Pandas, a Se...
-
3
pandas.DataFrame 转成 numpy.array 以及 C++ 的二进制文件 作者:
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK