The easiest way to convert an entire Pandas DataFrame to a NumPy array is using the ‘.to_numpy()’ method.
If your DataFrame contains multiple data type columns, the resulting NumPy array will have a common data type that can accommodate all of them, often resulting in an ‘object’ dtype.
import pandas as pd # Sample Data data = {'col1': [18, 19, 21], 'col2': [1.0, 3.0, 7.0], 'col3': ['K', 'B', 'Y']} # Creating a Data Frame df = pd.DataFrame(data) print("Original DataFrame:") print(df) # Displaying the DataFrame print(type(df)) # Displaying the type of the DataFrame # Converting DataFrame to NumPy array numpy_array = df.to_numpy() print("\nNumPy Array:") print(numpy_array) # Displaying the NumPy Array print(type(numpy_array)) # Displaying the type of the NumPy Array
Output
The above example is simple code converted into a numpy array, which you can verify by its data type.
Converting a specific column into a numpy array
We can directly convert specific columns of a DataFrame to a numpy array using df[‘column_name’].to_numpy() syntax.
import pandas as pd # Sample Data data = {'col1': [18, 19, 21], 'col2': [1.0, 3.0, 7.0], 'col3': ['K', 'B', 'Y']} # Creating a Data Frame df = pd.DataFrame(data) print("Original DataFrame:") print(df) # Displaying the DataFrame print(type(df)) # Displaying the type of the DataFrame # Converting "col2" of DataFrame numpy_array = df["col2"].to_numpy() print("\nNumPy Array:") print(numpy_array) # Displaying the NumPy Array print(type(numpy_array)) # Displaying the type of the NumPy Array
Output
The above code shows that we are transforming the “col2” of a DataFrame into a numpy array by specifying that column using the df[“col2”].numpy code snippet.
In the previous versions of numpy, we used the df.values() method. However, it was not as efficient as the df.to_numpy() method.
You can check out Converting Numpy Array to Pandas DataFrame.