Articles

Comvert numpy Array to pandas DataFrame

Comvert numpy Array to pandas DataFrame


In this tutorial, we will discuss how to convert  numpy array to pandas dataframe.

Introduction

DataFrame in pandas is two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.

 

We create DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.

Syntax:

pandas.DataFrame(data)

Where, data is the input dataframe , The data can be a dictionary that stores list of values with specified key.

 

numpy stands for numeric python which is used to perform mathematical operations on arrays.

It is a module in which we have to import from the python. 

Syntax to import:

import numpy

We can also use alias for the module 

For example,

import numpy as np

We can directly use np to call the numpy module.

Let's create an numpy array with 2D with 3 rows.

#import the numpy module
import numpy 

#create an 2D array
#with three rows
array_input=numpy.array([[1,2,3,4],
                        [34,32,44,22],
                        [43,45,32,56]])

#display array
print(array_input)

Output:

We created an array with integers with 3 rows and 4 columns

[[ 1  2  3  4]
 [34 32 44 22]
 [43 45 32 56]]

We can convert to the pandas dataframe by using pandas.DataFrame() method.

Syntax:

pandas.DataFrame(numpy_array_input,columns)

where,

  1. numpy_array_input is the input numpy array
  2. columns will take a list of column names to assign to the dataframe created

Example 1:

In this example,we will convert the above numpy array to the dataframe without columns.

#import the numpy module
import numpy 
#import pandas module
import pandas

#create an 2D array #with three rows array_input=numpy.array([[1,2,3,4], [34,32,44,22], [43,45,32,56]]) #create a dataframe from the numpy array data=pandas.DataFrame(array_input) #display the dataframe print(data)

Output:

    0   1   2   3
0   1   2   3   4
1  34  32  44  22
2  43  45  32  56

Example 2:

let's add column names - ['columnA','columnB','columnC','columnD'] to the converted dataframe.

#import the numpy module
import numpy 
#import pandas module
import pandas

#create an 2D array #with three rows array_input=numpy.array([[1,2,3,4], [34,32,44,22], [43,45,32,56]]) #create a dataframe from the numpy array with columns data=pandas.DataFrame(array_input,columns=['columnA','columnB','columnC','columnD']) #display the dataframe print(data)

Output:

   columnA  columnB  columnC  columnD
0        1        2        3        4
1       34       32       44       22
2       43       45       32       56

If we want the index names , then we can add the index to the converted dataframe using index parameter.

index=[index_names]

Example 3: 

let's add index names - ['A','B','C'] to the converted dataframe.

#import the numpy module
import numpy 

#import pandas module
import pandas

#create an 2D array
#with three rows
array_input=numpy.array([[1,2,3,4],
                        [34,32,44,22],
                        [43,45,32,56]])

#create a dataframe from the numpy array with columns
#with indices 
data=pandas.DataFrame(array_input,columns=['columnA','columnB','columnC','columnD'],index=['A','B','C'])

#display the dataframe
print(data)

Output:

   columnA  columnB  columnC  columnD
A        1        2        3        4
B       34       32       44       22
C       43       45       32       56

Pandas

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Jun 14,2024  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!