How to Convert Pandas DataFrame to NumPy Array in Python

In this Python Pandas tutorial, we will learn how to Convert Pandas DataFrame to NumPy Array in Python. Also, we will cover these topics.

  • Convert Pandas DataFrame to NumPy Array Without Header
  • Convert Pandas DataFrame to NumPy Array Without Index
  • Convert Pandas DataFrame to NumPy Array
  • Convert Pandas Series to NumPy Array
  • Convert Pandas DataFrame to 3d NumPy Array
  • Convert Pandas DataFrame to 2d NumPy Array
  • Convert Pandas DataFrame to NumPy Matrix

We have used Steam Player Data that is downloaded from Kaggle.

Convert Pandas DataFrame to NumPy Array Without Header

In this section, we will learn how to convert pandas dataframe to Numpy array without header in Python.

  • using dataframe.to_numpy() method we can convert any dataframe to a numpy array.
  • Numpy array generated after this method do not have headers by default.
  • Though the header is not visible but it can be called by referring to the array name. Array name will be the column names like ‘Month_Year’, ‘Gain’, ‘URL’, etc in ths case.
  • Here is the implementation on Jupyter Notebook.

Read: How to Find Duplicates in Python DataFrame

Convert Pandas DataFrame to NumPy Array Without Index

In this section, we will learn how to convert a dataframe t a Numpy array without an Index in Python.

  • Using dataframe.to_records(index=False) we can convert pandas dataframe to Numpy array without index.
  • In our implementation on Jupyter Notebook we have demonstrated both with and without index so that you can mark the difference.
  • In case you still have doubt, please try after changing the index column.

Read: Count Rows in Pandas DataFrame

Convert Pandas DataFrame to NumPy Array

In this section, we will learn how to convert pandas dataframe to Python Numpy array.

  • Numpy array is a grid values having same data types.
  • Using dataframe.to_numpy() method we can convert pandas dataframe to Numpy array.
  • Here is the implementation on Jupyter Notebook.

Read: Python Pandas DataFrame Iterrows

Convert Pandas Series to NumPy Array

In this section, we will learn how to convert series to Numpy array in Python.

  • Series is a one dimensional array with unique lables.
  • using series.to_numpy() method in Python Pandas we can convert pandas series to Numpy array.
  • In our implementation on Jupyter Notebook we have created a simple series and then convert it into a Numpy array.

Read: Get index Pandas Python

Convert Pandas DataFrame to 3D NumPy Array

In this section, we will learn how to convert Pandas dataframe to 3D Numpy array in Python.

  • Pandas dataframe can be converted to numpy using method dataframe.to_numpy() but this won’t be sufficient.
  • To covert dataframe to 3D Numpy array we have to use addition method reshape() with dataframe.to_numpy() method.
  • So the complete statement to convert pandas dataframe to 3d numpy array would be:
dataframe.to_numpy().reshape(2, 3, 4)
  • Thing to understand here is that the size of array must the multiplication of 3 numbers that are provided inside the reshape.
  • For example, right now value inside reshape is 2 x 3 x 4 = 24, so the the size of array must be 24 in this case.
  • But the example we have used is a real dataset downloaded by kaggle. It has 42168 array size. Now the 3 numbers should be such that if multiplied together then it should be 42168.
  • So we have taken reshape(7028, 3, 2), which ultimately becomes 7028 x 3 x 3 = 42168. Hence, condition is satisfied and our 3D numpy array is formed.
  • Here is the implementation on Jupyter Notebook. Please read comments to understand every step.

Read: Python Pandas Write DataFrame to Excel

Convert Pandas DataFrame to 2D NumPy Array

In this section, we will learn how to convert pandas dataframe to 2D Numpy array in Python.

  • We have seen multiple examples of 2D Numpy array in our this tutorial.
  • Any array with two dimensions is a 2D Numpy array.
  • Using dataframe.to_numpy() we can convert any dataframe to 2D Numpy array.
  • Here is the implementation on Jupyter Notebook.

Read: Python DataFrame to CSV

Convert Pandas DataFrame to NumPy Matrix

Let us see, how to convert pandas dataframe to Numpy matrix in Python.

  • A two-dimensional rectangular array to store data in rows and columns is called python matrix.
  • Matrix is a Numpy array to store data in rows and columns.
  • Using dataframe.to_numpy() method we can convert dataframe to Numpy Matrix.
  • Apart from this, using dataframe.values we can convert the dataframe to Numpy Matrix in Python Pandas.
  • Earier there use to be a method dataframe.as_matrix() using which dataframe is converted to Numpy matrix but now it is depreciated and inplace of that dataframe.values is used.

You may also like to read the following tutorials.

In this tutorial, we have learned how to convert Pandas DataFrame to NumPy Array in Python. Also, we have covered these topics.

  • Convert Pandas DataFrame to NumPy Array Without Header
  • Convert Pandas DataFrame to NumPy Array Without Index
  • Convert Pandas DataFrame to NumPy Array
  • Convert Pandas Series to NumPy Array
  • Convert Pandas DataFramee to 3d NumPy Array
  • Convert Pandas DataFrame to 2d NumPy Array
  • Convert Pandas DataFrame to NumPy Matrix