Open In App

Convert Numpy Array to Dataframe

Last Updated : 05 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Data manipulation and analysis are common tasks in the field of data science, and two powerful libraries in Python that facilitate these tasks are NumPy and Pandas. NumPy provides support for large, multi-dimensional arrays and matrices, while Pandas offers data structures like DataFrames that make it easy to manipulate and analyze structured data. In this article, we’ll explore how to create a Pandas DataFrame from a NumPy array.

Create Dataframe from Numpy Array in Python

Below are some of the ways by which we can understand how we can convert a NumPy Array to Pandas DataFrame in Python:

  • Using pd.DataFrame()
  • Specifying Column Names
  • Customize Row and Column Indices
  • Using pd.DataFrame.from_records()

Create Numpy Array

In this example, the below code imports the NumPy and Pandas libraries and creates a 2D NumPy array named “data” containing a matrix, which is then printed.

Python3




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.

Python3




# 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.

Python3




#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.

Python3




# 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.

Python3




# 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

Conclusion

In conclusion, each of these approaches provides flexibility for creating a Pandas DataFrame from a NumPy array, and the choice depends on your specific requirements and the nature of your data. Whether you need to customize indices, specify column names, or work with structured data, Pandas offers multiple ways to seamlessly convert NumPy arrays into versatile DataFrames.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads