Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

Converting Pandas DataFrame to Numpy Array

  • 24 Jan, 2025
  • Com 0
Featured Image of Converting Pandas DataFrame to Numpy Array

The easiest way to convert an entire Pandas DataFrame to a NumPy array is using the ‘.to_numpy()’ method.

If your DataFrame contains multiple data type columns, the resulting NumPy array will have a common data type that can accommodate all of them, often resulting in an ‘object’ dtype.

import pandas as pd

# Sample Data
data = {'col1': [18, 19, 21],
        'col2': [1.0, 3.0, 7.0],
        'col3': ['K', 'B', 'Y']}

# Creating a Data Frame
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)  # Displaying the DataFrame
print(type(df))  # Displaying the type of the DataFrame

# Converting DataFrame to NumPy array
numpy_array = df.to_numpy()
print("\nNumPy Array:")
print(numpy_array)  # Displaying the NumPy Array
print(type(numpy_array))  # Displaying the type of the NumPy Array

Output

Converting Pandas DataFrame to Numpy Array

The above example is simple code converted into a numpy array, which you can verify by its data type.

Converting a specific column into a numpy array

We can directly convert specific columns of a DataFrame to a numpy array using df[‘column_name’].to_numpy() syntax.

import pandas as pd

# Sample Data
data = {'col1': [18, 19, 21],
        'col2': [1.0, 3.0, 7.0],
        'col3': ['K', 'B', 'Y']}

# Creating a Data Frame
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)  # Displaying the DataFrame
print(type(df))  # Displaying the type of the DataFrame

# Converting "col2" of DataFrame
numpy_array = df["col2"].to_numpy()
print("\nNumPy Array:")
print(numpy_array)  # Displaying the NumPy Array
print(type(numpy_array))  # Displaying the type of the NumPy Array

Output

Converting a specific column into a numpy array

The above code shows that we are transforming the “col2” of a DataFrame into a numpy array by specifying that column using the df[“col2”].numpy code snippet.

In the previous versions of numpy, we used the df.values() method. However, it was not as efficient as the df.to_numpy() method.

You can check out Converting Numpy Array to Pandas DataFrame.

Post Views: 44
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Use Ajax in Laravel 11
How to Fix Composer is operating significantly slower curl php

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend