Convert pandas dataframe to NumPy array
How do I convert a pandas dataframe into a NumPy array?
Use df.to_numpy()
df.to_numpy() is the recommended method to convert a pandas dataframe into a NumPy array. It is defined on Index, Series, and DataFrame objects and provides a consistent way to extract the underlying NumPy array.
import pandas as pd import numpy as np df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) # Convert the entire DataFrame array = df.to_numpy() # Convert specific columns array = df[['A', 'C']].to_numpy()
The returned array is a view of the original DataFrame, so any modifications made to the array will be reflected in the DataFrame. To obtain a copy instead, use to_numpy(copy=True).
Preserving the dtypes
If you need to preserve the dtypes in the result, you can use DataFrame.to_records() instead of to_numpy().
record_array = df.to_records()
However, note that to_records() returns a rec.array, which is not a NumPy array. Instead, it is a structured array that is more akin to a pandas DataFrame. If you need a true NumPy array with dtypes preserved, you can use np.rec.fromrecords():
named_array = np.rec.fromrecords(v, names=v.columns.tolist())
The above is the detailed content of How to Convert a Pandas DataFrame to a NumPy Array?. For more information, please follow other related articles on the PHP Chinese website!