Converting Pandas DataFrame to NumPy Array and Vice Versa

In this tutorial, we are going to learn and understand: Converting Pandas DataFrame to NumPy Array and Vice Versa.

Introduction

Sometimes, you may need to convert a Pandas DataFrame into NumPy array for numerical operations. At other times, you may need to turn it into DataFrame for easier manipulation. In this tutorial, we’ll cover both conversions step by step.

Why Convert Between Pandas and NumPy?

Some reasons for this include:

  • NumPy arrays can be faster for numerical computations.
  • Pandas DataFrames can offer better data manipulation with built-in functions.
  • Some libraries work better with NumPy, while data analysis tools works better with Pandas.

Step 1: Importing Required Libraries

We first need to import Pandas and NumPy to handle our dataset.

import pandas as pd
import numpy as np

Step 2: Creating Sample DataFrame

Let us now create a DataFrame with sample data:

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

Its output will be:

   A  B  C
0  1  4  7
0  2  5  8
0  3  6  9

Step 3: Converting DataFrame to NumPy Array

We convert DataFrame into NumPy Array using .to_numpy() method:

numpy_array = df.to_numpy()

Its output will be:

[[1 4 7]
 [2 5 8]
 [3 6 9]]

Step 4: Converting NumPy Array to DataFrame

We convert NumPy Array into DataFrame using pd.DataFrame() method:

df_from_array = pd.DataFrame(numpy_array, columns=['A', 'B', 'C'])

Its output will be:

   A  B  C
0  1  4  7
0  2  5  8
0  3  6  9

Step 5: Displaying Results

The total result can be printed using

print("Original DataFrame:")
print(df)
print("\nConverted NumPy Array:")
print(numpy_array)
print("\nDataFrame converted back from NumPy Array:")
print(df_from_array)

Its output will be:

Original DataFrame:
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9

Converted NumPy Array:
[[1 4 7]
 [2 5 8]
 [3 6 9]]

DataFrame converted back from NumPy Array:
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9

Conclusion

In this tutorial we learned how to:

  • Convert a Pandas DataFrame to a NumPy array using .to_numpy().
  • Convert a NumPy array back to a Pandas DataFrame using pd.DataFrame().

Leave a Comment

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

Scroll to Top