Converting NumPy arrays to Pandas DataFrame – Examples included

Python Pandas @ Freshers.in

Understanding data structures in Python is crucial for data analysis. Two popular libraries, NumPy and Pandas, offer powerful tools for this purpose. Sometimes, you may find the need to convert between their primary data structures: the NumPy array and the Pandas DataFrame. In this article, we’ll dive deep into how you can easily convert a NumPy array into a DataFrame with detailed examples.

Creating a NumPy Array

Let’s first create a simple NumPy array using the numpy library. For our example, we’ll use data related to five individuals: Sachin, Ram, Abhilash, Mike, and Elaine.

import numpy as np

# Sample data: Age and Score for Sachin, Ram, Abhilash, Mike, and Elaine
data_array = np.array([
    [25, 85],
    [30, 88],
    [29, 76],
    [24, 90],
    [27, 82]
])
print(data_array)
Converting NumPy Array to DataFrame
Using pandas, converting a NumPy array to a DataFrame is straightforward. Here’s how you can do it:
import pandas as pd

# Convert NumPy array to DataFrame
df = pd.DataFrame(data_array, columns=['Age', 'Score'], index=['Sachin', 'Ram', 'Abhilash', 'Mike', 'Elaine'])

print(df)

The columns argument helps us set the column names, while the index argument sets row labels for our data.

Refer more on python here :
Refer more on python here : PySpark 

Author: user