Convert Pandas DataFrame to Numpy array

For performing some high-level mathematical functions, we can convert Pandas DataFrame to numpy arrays. It uses the DataFrame.to_numpy() function.

The DataFrame.to_numpy() function is applied on the DataFrame that returns the numpy ndarray.

Syntax

Parameters

  • dtype: It is an optional parameter that pass the dtype to numpy.asarray().
  • copy: It returns the boolean value that has the default value False.

It ensures that the returned value is not a view on another array.

Returns

It returns the numpy.ndarray as an output.

Example1

Output

array([[2, 4.0, Timestamp('2000-01-01 00:00:00')],
       [3, 5.8, Timestamp('2000-01-02 00:00:00')]], dtype=object)

Example2

Output

DataFrame
----------
   x    y   z
0  17  62  35
1  25  36  54
2  42  20  15
3  48  62  76

Numpy Array
----------
 [[17 62 35]
 [25 36 54]
 [42 20 15]
 [48 62 76]]





Latest Courses