The Numpy and Pandas are the amazing libraries available in Python for computations and data analysis. You can convert a Numpy array to multiple formats. In the previous article, we have come across how we can convert a Numpy array to a CSV file. Now, in this article, we will focus on how we can convert Numpy array to dataframe in python.

## Steps to follow

• Importing the libraries – You have to import the Numpy and Pandas libraries to begin.
• Creating an Array – Now you need to create an Numpy array with multiple values.
• Create a list – You have to create a list of index and column values.
• Create dataframe – After the index and column value list you have to create a dataframe using pandas.
• Finally – Print the dataframe.

## 1. Convert Numpy array to dataframe (Example 1)

In this first example, we will see how we can convert array to dataframe in python using Numpy and Pandas libraries as well.

```#Importing libraries
import numpy as np
import pandas as pd

#creates an array
test_array = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])

#index values
ind_val = ['one','two','three','four']

#Columns values
col_val = ['col_1','col_2','col_3']

#creates a dataframe
test_df = pd.DataFrame(data=test_array,index=ind_val,columns=col_val)

#Prints the dataframe
test_df```

Well, in the above code, we first imported required libraries i.e. Nympy and Pandas. Later we have created an array and then using pandas library we have converted array to dataframe easily.

## 2. Convert Numpy array to dataframe (Example 2)

Let us look over another example to know how yo create a dataframe from a Numpy array in python with fewer lines of code.

Let’s see how it works!

```#Importing required libraries
import numpy as np
import pandas as pd

#Creates an array
test_array = np.array([['Ayisha',21],['Raj',20],['Jessie',22],['Brown',22]])

#Creates index values
ind_val = ['1','2','3','4']

#Creates column values
col_val = ['Student','Age']

#Creates a dataframe
test_df = pd.DataFrame(data=test_array,index=ind_val,columns=col_val)

#Prints the dataframe
test_df```

This is just another example where we have created an array and converted it to a dataframe. In this, we have used two columns with student name and age as column names and 1,2,3 and 4 as row names or the index values.

## Conclusion

You can do almost everything using the Numpy library in Python. You can convert Numpy array to multiple formats.

In this article, we know about how we can convert array to dataframe. Just as we showed above outputs and examples try creating your own array and convert to a dataframe.

That’s all for now. Happy learning!!!

More study: Numpy.org

Categorized in:

Tagged in:

, ,