There are two ways to convert a pandas DataFrame to a NumPy array:
- 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
andcopy
.dtype
specifies the desired data type of the output array, andcopy
specifies whether to make a copy of the DataFrame data or to return a view on the existing data. - Using the
df.values
attribute: This attribute returns a NumPy array view of the DataFrame data. It is important to note that thedf.values
attribute may be deprecated in future versions of pandas, so it is recommended to use thedf.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!