Convert Numpy Array to Dataframe
Converting a NumPy array into a Pandas DataFrame makes our data easier to understand and work with by adding names to rows and columns and giving us tools to clean and organize it.
In this article, we will take a look at methods to convert a numpy array to a pandas dataframe. We will be discussing two such methods and implement them.
- Using pd.DataFrame()
- Using pd.DataFrame.from_records()
Before we begin with the conversion, we need to create a NumPy array.
Creating a NumPy Array
The below code imports the NumPy and Pandas libraries and creates a 2D NumPy array named "data" containing a matrix.
import numpy as np
import pandas as pd
# Create a NumPy array
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(data)
Output:
[[1 2 3]
[4 5 6]
[7 8 9]]
Methods 1: pd.DataFrame()
The most straightforward method is to use the Pandas pd.DataFrame() constructor, passing the NumPy array as an argument.
# Using pd.DataFrame()
df1 = pd.DataFrame(data)
# Display the DataFrame
print(df1)
Output:
0 1 2
0 1 2 3
1 4 5 6
2 7 8 9
Specifying Column Names
You can provide column names while creating the DataFrame using the columns parameter.
#Specifying Column Names
df2 = pd.DataFrame(data, columns=['col1', 'col2', 'col3'])
# Display the DataFrame
print(df2)
Output:
col1 col2 col3
0 1 2 3
1 4 5 6
2 7 8 9
Customize Row and Column Indices
Customize both row and column indices using the index and columns parameters.
# Approach 3: Customizing Row and Column Indices
df3 = pd.DataFrame(data, index=['row1', 'row2', 'row3'], columns=['col1', 'col2', 'col3'])
# Display the DataFrame
print(df3)
Output:
col1 col2 col3
row1 1 2 3
row2 4 5 6
row3 7 8 9
Methods 2: pd.DataFrame.from_records()
Another way is to use pd.DataFrame.from_records() method, which is particularly useful when dealing with structured data or records.
# Using pd.DataFrame.from_records()
df4 = pd.DataFrame.from_records(data, columns=['col1', 'col2', 'col3'])
# Display the DataFrame
print(df4)
Output:
col1 col2 col3
0 1 2 3
1 4 5 6
2 7 8 9