Skip to content

Convert pandas dataframe to NumPy array

In pandas, you can convert a DataFrame to a NumPy array by using the values attribute. Note that .values is deprecated in pandas 1.0+; to_numpy() is the recommended approach.

Python: Convert a DataFrame to a NumPy array by using the values attribute

python
import pandas as pd

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

numpy_array = df.values

print(*numpy_array)

This will return a 2-dimensional NumPy array containing all the data in the DataFrame. The shape of the array will be (n,m) where n is the number of rows and m is the number of columns in the DataFrame.

Alternatively, you can use the to_numpy() method which also returns a NumPy array containing the data in the DataFrame.

Python: Convert a DataFrame to a NumPy array by using the to_numpy() method

python
numpy_array = df.to_numpy()

You can also convert specific columns of the DataFrame to a NumPy array by passing the column name to the values attribute or to_numpy() method.

Python: Convert specific columns of a DataFrame to a NumPy array by using the to_numpy() method

python
numpy_array = df['A'].values
numpy_array = df[['A','B']].to_numpy()

Please keep in mind that the resulting NumPy array will not have the same column names as the original DataFrame. If you need the column names, you can use the .columns attribute of the DataFrame. Note that for mixed-type columns, the resulting array will be upcast to a common dtype (typically object).

Dual-run preview — compare with live Symfony routes.