Convert pandas dataframe to NumPy array

In pandas, you can convert a DataFrame to a NumPy array by using the values attribute.

import pandas as pd

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

numpy_array = df.values


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.

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

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.