# Convert Pandas DataFrame to NumPy Array in Python

**PyShark**, and kindly contributed to python-bloggers]. (You can report issue about the content on this page here)

Want to share your content on python-bloggers? click here.

In this article we will discuss how to convert Pandas DataFrame to NumPy array in Python.

**Table of Contents**

- Introduction
- Create a sample Pandas DataFrame
- Convert Pandas DataFrame to NumPy array
- Conclusion

## Introduction

We are used to working with DataFrames in Python for a lot of functionality provided by the pandas library. It is very efficient for any of the data manipulation tasks we do when preparing the features for machine learning models. However, sometimes we want to extend the functionality and make use of some NumPy mathematical functions. In this case, we can’t work with Pandas DataFrames and need to convert them to NumPy arrays.

In this tutorial we will explore how we can easily convert our data to an array for the user to work with more mathematical functions. The pandas library has a very useful method which we will discuss. It makes the conversion a very simple one line code, but allows us to perform more extensive work on the data analysis.

To continue following this tutorial we will need the following Python library: pandas.

If you don’t have it installed, please open “Command Prompt” (on Windows) and install it using the following code:

pip install pandas

## Create a sample Pandas DataFrame

As the first step we will create a sample Pandas DataFrame that we will later convert to a NumPy array.

import pandas as pd df = pd.DataFrame( { "Education" : [5, 5, 7], "Experience" : [1, 3, 8], "Salary" : [ 40000, 50000, 80000] } )

This DataFrame will have 3 observations with three columns: Education, Experience, and Salary.

Let’s take a look at the data:

print(df)

And we should get:

Education Experience Salary 0 5 1 40000 1 5 3 50000 2 7 8 80000

## Convert Pandas DataFrame to NumPy array

Converting a Pandas DataFrame to a NumPy array is very simple using **.to_numpy()** Pandas method. It parses a DataFrame and converts it to an array:

np_array = df.to_numpy()

Let’s take a look:

print(np_array)

And we should get:

[[ 5 1 40000] [ 5 3 50000] [ 7 8 80000]]

## Conclusion

In this article we discussed how to convert Pandas DataFrame to NumPy array in Python using pandas library.

Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Python Programming articles.

The post Convert Pandas DataFrame to NumPy Array in Python appeared first on PyShark.

**leave a comment**for the author, please follow the link and comment on their blog:

**PyShark**.

Want to share your content on python-bloggers? click here.