How to Convert Pandas DataFrame to NumPy Array in Python?

In Python, “pandas” is a very powerful and widely used library for working with data. The “DataFrame” object is at the core of this functionality. The DataFrame object has many useful methods for manipulating and analyzing data, such as the head(), tail(), sort_tables(), etc.

Similarly, “NumPy” is an essential library for scientific computing with Python and is used in various applications, including Machine Learning. Signal processing. For more efficient numerical or performing operations on a large dataset, we must convert pandas DataFrame to Numpy Array. For conversion, various inbuilt functions are used in Python, such as “DataFrame.to_numpy()”, “DataFrame.values()”, etc.

This post will demonstrate various methods that help to convert the pandas DataFrame to a Numpy array using the examples given below:

Let’s begin with the first method.

Method 1: Using DataFrame.to_numpy() Function

The “DataFrame.to_numpy()” function is used in Python to convert the input Python pandas DataFrame into a Numpy array. An example:

Example 1: Converting Complete Python Pandas DataFrame

The panda’s module “pd.dataframe()” function is used in Python to generate a dataframe. To convert the Python dataframe into a NumPy array, the “DataFrame.to_numpy()” function is used in the below code:

Code:

import pandas as pd
Data = pd.DataFrame([['Alex', 12, 6.7], ['Lily', 28, 6.9],
    ['Joseph', 22, 5.9]], columns=['Name', 'Age', 'Height'])

print('DataFrame: \n\n', Data)
numpy_array = Data.to_numpy()
print('\n\n Numpy Array: \n\n', numpy_array)

In the above code, the “pd.DataFrame()” function accepts the DataFrame row separated by commas and initialized as a list as a first argument. The name of the DataFrame columns is initialized as a list in the second argument. The “DataFrame.to.numpy()” function converts the given DataFrame into a Numpy array.

Output:

The above output shows the DataFrame and converted NumPy array value.

Example 2: Converting Specific Columns of DataFrame

In the given example code, the “DataFrame.to_numpy()” function will be used to convert the specific columns of Pandas DataFrame: :

Code:

import pandas as pd
Data = pd.DataFrame([['Alex', 12, 6.7], ['Lily', 28, 6.9],
    ['Joseph', 22, 5.9]], columns=['Name', 'Age', 'Height'])

print('DataFrame: \n', Data)
numpy_array = Data[['Name', 'Height']].to_numpy()
print('\n Numpy Array: \n', numpy_array)

In the above code, the “Data.to_numpy()” function accepts the column name of DataFrame as an argument. This function returns the Numpy value for the given specific data frame columns.

Output:

The above output verified that the specific columns of DataFrame have been converted into Numpy arrays using the “Data.to_numpy()” function.

Method 2: Using DataFrame.values()

The “DataFrame.values()” function is also utilized in the program to convert the DataFrame into a Numpy array. Here is an example code:

Code:

import pandas as pd
Data = pd.DataFrame([['Alex', 12, 6.7], ['Lily', 28, 6.9],
    ['Joseph', 22, 5.9]], columns=['Name', 'Age', 'Height'])

print('DataFrame: \n', Data)
numpy_array = Data.values
print('\n Numpy Array: \n', numpy_array)

In the above code, the “Data.values” function is assigned to variables “numpy_array”. This function retrieves the DataFrame into a Numpy array.

Output:

The above output verified that the DataFrame was successfully converted into a NumPy array.

These are all the possibilities to convert DataFrame to NumPy in Python.

Conclusion

To convert the pandas DataFrame to a NumPy array, the functions “DataFrame.to_numpy()” and the “DataFrame.values()” are used in Python. The “DataFrame.to_numpy()” function is used in Python to convert the complete “DataFrame” into “Numpy”. This function is also utilized to convert the specific columns of the data frame into numpy. This article presented a detailed guide on converting the pandas DataFrame to a Numpy array utilizing various examples.