Numpy and Pandas are the most used Python libraries in the field of data science or AI. If you want to convert NumPy array to dataframe then this entire tutorial is for you. I will show how to create a Numpy Array and convert it to Dataframe and dataframe to Numpy array.
Syntax to Convert Numpy Array to Dataframe
There is a method in Pandas library pandas.Dataframe() that allows you to convert NumPy array to data frame. It accepts the following arguments.
data: Your NumPy array.
index : To decide which column you want to act as the index.
columns: All columns name or list in array.
dtype: Type of the value of the array.
copy: Use to make duplicates of the input array.
pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)
Steps by Steps to convert Numpy array to dataframe
Step 1: Import all the required libraries.
In this entire tutorial, only pandas and NumPy is being used. So let’s import these libraries using the below code.
import numpy as np import pandas as pd
Step 2: Create a Numpy array
Let’s create a NumPy array for the demonstration purpose using the method numpy.array().
Step 3: Convert the numpy array to the dataframe.
The easiest way to convert the NumPy array is by using pandas. The Pandas has a method that allows you to do so that is pandas.DataFrame() as I have already discussed above its syntax. Let’s convert it.
df = pd.DataFrame(data) print(df)
How to Add Name to Each Column?
If you want to add the column name instead of 0,1 or 2 then you have to pass the columns name as a list inside the pandas.DataFrame() method.
df2 = pd.DataFrame(data,columns=["c1","c2","c3"]) print(df2)
The output will be like this.
Add Names of the Rows
You can also add the name of each row in the dataframe. Just you have to pass the index list as an argument inside the pandas.Dataframe() method.
df3 = pd.DataFrame(data,columns=["c1","c2","c3"],index=["row1","row2"]) print(df3)
The output will contain the name of each row and column of the dataframe.
Other things you can do with Dataframe
If you want to change the name of each column then you will have to use the dot operator on the dataframe.
# modify column name print(df3.shape) df3.columns = ["A", "B", "C"] print(df3)
Here you can see I am first checking the number of columns using the shape and after that assigning the new column names using df3.columns.
Conversion of the numpy array to dataframe is the best way for manipulating the dataset. These are the steps to change a NumPy array to dataframe. Hope you have liked this article if you have any query regarding this then you can contact us for more information.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.