Pandas DataFrame to NumPy Array

In this post we will see how to convert Convert Pandas DataFrame to NumPy Array.

You can convert Pandas DataFrame to Numpy Array to perform mathematical computation supported by NumPy library.

You can use DataFrame.to_numpy() to convert Pandas DataFrame to NumPy Array.

Syntax of DataFrame.to_numpy()

Parameters

dtype: Data type for target numpy array.
copy: [bool, default False] Whether to ensure that the returned value is a not a view on another array.

Return

DataFrame.to_numpy() returns NumPy.ndArray.

Example 1: Convert Pandas DataFrame to NumPy Array

Here is simple example to convert Pandas DataFrame to NumPy Array.

Output

Example 2: Convert Pandas DataFrame to NumPy Array with mix dtypes

If you have dataframe with mixed dtypes, then NumPy array will be created of lowest dataTypes.

For example:
Python DataFrame with int64 qnd float64 will be convert to NumPy array of type float64

Output

That’s all about How to convert Pandas DataFrame to NumPy Array

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *