Convert Pandas Dataframe to Numpy Array with Examples

Convert Pandas Dataframe to Numpy Array

I have already completed a dedicated post on how to Convert Numpy Array to Dataframe. But a lot of our readers have asked to explain to convert pandas dataframe to NumPy array also. So Here We have come. In this entire tutorial, you will know how to convert pandas dataframe to a NumPy array using two methods.

  1. dataframe.values
  2. dataframe.to_numpy()

Step by Step Guide to convert pandas dataframe to NumPy array

Step 1: Import all the necessary libraries.

Here I am using only the pandas python module. Let’s import them.

import pandas as pd

Step 2: Create a DataFrame.

Lets create a sample pandas dataframe for this example. To create that first I am using pandas.DataFrame() method. Use the following code.

# df creation
    people = {"Name": ["Sahil", "Ravish", "John", "Abhishek", "Rohan"],
              "Age": [25, 27, 20, 26, 21], "Year": [2018, 2017, 2019, 2018, 2015]}
    df = pd.DataFrame(people)
    print(df)

Output

Sample DataFrame for converting to Numpy Array
Sample DataFrame for converting to Numpy Array

Step 3: Convert pandas dataframe to numpy array.

Now it’s time for converting datframe  to NumPy array. There are two methods you can achieve it.

Example 1: Conversion using dataframe.values

In this method, you have to only use the dot operation for the dataframe you want to convert. Use the following code.

numpy_array = df.values
print(numpy_array)
print("############################################")
print(type(numpy_array))
Printing and Verifying the Type of Object after Conversion
Printing and Verifying the Type of Object after Conversion

You can verify the type of the object using the type() method. Here if you will see the output of the above code., it clearly shows that it is NumPy array.

Example 2:  Using dataframe.to_numpy() method.

The second method is to convert pandas dataframe to NumPy array is using the to_numpy() method. Here You will get the same output as in example 1. Execute the following code.

numpy_array2 = df.to_numpy()
print(numpy_array2)
print("############################################")
print(type(numpy_array2))
Printing and Verifying the Type of Object after Conversion using to_numpy() method
Printing and Verifying the Type of Object after Conversion using to_numpy() method

You will get the same type of the object that is NumPy array.

 

Other Examples

If you want to convert the dataframe to numpy array of a single column then you can also do so. The method is the same. First pass the Name of the column inside the square bracket. Then use the to_numpy() method. Look at the following code.

age = df["Age"].to_numpy()
print(age)
print("############################################")
print(type(age))

You will get the output as below.

Printing and Verifying the Type of Object after Conversion for Single Column
Printing and Verifying the Type of Object after Conversion for Single Column

End Notes

The above examples to convert pandas dataframe to NumPy array uses the two methods. Starting from the new versions of pandas dataframe.values may get depreciated. Therefore I will suggest you use to_numpy() method for conversion.

Hope this tutorial has cleared all the queries. Even if you have any doubt then you can contact us for more information.

Full Code

import pandas as pd


def main():
    # df creation
    people = {"Name": ["Sahil", "Ravish", "John", "Abhishek", "Rohan"],
              "Age": [25, 27, 20, 26, 21], "Year": [2018, 2017, 2019, 2018, 2015]}
    df = pd.DataFrame(people)
    print(df)
    print("############################################")
    numpy_array = df.values
    print(numpy_array)
    print("############################################")
    print(type(numpy_array))
    print("############################################")
    numpy_array2 = df.to_numpy()
    print(numpy_array2)
    print("############################################")
    print(type(numpy_array2))

    print("############################################")
    age = df["Age"].to_numpy()
    print(age)
    print("############################################")
    print(type(age))


if __name__ == '__main__':
    main()

Output

Output for converting pandas dataframe to numpy array
Output for converting pandas dataframe to NumPy array

Source

Official Pandas Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner