Skip to content

pandas to numpy

There are two ways to convert a pandas DataFrame to a NumPy array:

  1. Using the df.to_numpy() method: This is the recommended method, as it is the most efficient and flexible. It takes two optional parameters: dtype and copy. dtype specifies the desired data type of the output array, and copy specifies whether to make a copy of the DataFrame data or to return a view on the existing data.
  2. Using the df.values attribute: This attribute returns a NumPy array view of the DataFrame data. It is important to note that the df.values attribute may be deprecated in future versions of pandas, so it is recommended to use the df.to_numpy() method instead.

Here is an example of how to use the df.to_numpy() method to convert a pandas DataFrame to a NumPy array:

Python

import pandas as pd
import numpy as np

df = pd.DataFrame({'col1': [1, 2, 3], 'col2': [4, 5, 6]})

# Convert the DataFrame to a NumPy array
np_array = df.to_numpy()

# Print the NumPy array
print(np_array)

Output:

[[1 4]
 [2 5]
 [3 6]]

You can also use the df.to_numpy() method to convert individual columns of a DataFrame to NumPy arrays. For example, to convert the col1 column to a NumPy array, you would use the following code:

Python

np_array = df['col1'].to_numpy()

# Print the NumPy array
print(np_array)

Output:

[1 2 3]

I hope this helps!

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)