Convert NumPy Array to Pandas Dataframe

This article will discuss how to convert Numpy arrays to a Pandas DataFrame.

Table of Contents

A DataFrame is a data structure that will store the data in rows and columns. We can create a DataFrame using pandas.DataFrame() method. Numpy Array is an array data structure in Python, useful for scientific computing.

Convert Numpy Array to Dataframe using pandas.DataFrame()

We can convert the Numpy Array to pandas dataframe using DataFrame() method. This is a method used to convert the dataframe available in pandas. So we have to import the pandas module.

Syntax is as follows:

Advertisements
pandas.DataFrame(array_name,columns,index)

where,

  • array_name is the input numpy array which should be two dimensional array
    [[elements],[elements],……….,[elements]]
    • The elements define the number of columns in the dataframe and the number of arrays define the number of rows.
  • columns are used to specify the columns in the dataframe which are taken in the form of list separated by comma.
    • [‘column_name1′,””””,’column_name n’]
  • index is used to specify the rows in the dataframe which are taken in the form of list separated by comma.
    • [‘row_name1′,””””,’row_name n’]

Let’s create our numpy array with 5 arrays with 2 elements each

#import numpy module
import numpy

#create numpy array with 5 data of students
array=numpy.array([ ['sravan',7058],
                    ['ramya',7054],
                    ['harsha',7072],
                    ['bobby',7053],
                    ['kyathi',7088]])

#display
print(array)

Output:

[['sravan' '7058']
 ['ramya' '7054']
 ['harsha' '7072']
 ['bobby' '7053']
 ['kyathi' '7088']]

Convert Numpy Array to pandas dataframe with default row/column labels

Here In this example we are simply converting the above array to a Pandas DataFrame.

#import pandas module
import pandas

#create pandas dataframe from numpy array
data=pandas.DataFrame(array)

#display
print(data)

Output:

        0     1
0  sravan  7058
1   ramya  7054
2  harsha  7072
3   bobby  7053
4  kyathi  7088

Convert Numpy Array to Pandas Dataframe with Column and Row Names

Here In this example we are simply converting the above array to Pandas DataFrame and specift rows and columns

#import pandas module
import pandas

# create pandas dataframe from numpy array by specifying rows and columns
# row name starts from  row1 to row5
# Column names are 'Name'and 'Roll no'
data=pandas.DataFrame(  array,
                        columns=['Name','Roll no'],
                        index=['row1','row2','row3','row4','row5'])

#display
print(data)

Output:

        Name Roll no
row1  sravan    7058
row2   ramya    7054
row3  harsha    7072
row4   bobby    7053
row5  kyathi    7088

Here we are specifying column names as Name and Roll no and rows as row1 to row5.

Convert 2D Numpy Array to Pandas DataFrame

Here we are going to consider an two dimensional numpy array and convert into a Dataframe. A 2D Numpy array has n rows and n columns . we can convert to dataframe by using these rows and columns. So these will form a row and column in pandas dataframe.

First we will create an two dimensional numpy array for a range of integers using arange() function with 2 rows and 5 columns.

#import numpy module
import numpy

#create 10 elements with 2 rows and 5 columns
array= numpy.arange(10).reshape(2,5)

#display
print(array)

Output:

[[0 1 2 3 4]
 [5 6 7 8 9]]

Now, we will convert into pandas dataframe.

#import pandas
import pandas as pd

#convert the numpy array to pandas dataframe
data=pd.DataFrame(  array,
                    columns=['col1','col2','col3','col4','col5'],
                    index=['row1','row2'])

#display
print(data)

Output:

      col1  col2  col3  col4  col5
row1     0     1     2     3     4
row2     5     6     7     8     9

Here we specified the row names as row 1 to row n and column names as col1 to col n.

Convert 2D Numpy Array tp Dataframe with different types

Here we will create a two-dimensional numpy array with different data types and convert it into a dataframe. The 2D Numpy array has n rows and n columns. We can convert it to a dataframe. These rows and columns of the 2D Numpy Array will be the rows and columns of the pandas Dataframe.

Let’s create a two-dimensional numpy array using a set of integers with one array as int type and another as column type and convert it into dataframe

#import numpy module
import numpy

#create 10 elements with 2 rows and 5 columns
array= numpy.array([[23, 45, 43, 23, 21],
                    [45.6, 32.5, 45.6, 6.7, 8.9]])

#display
print(array)

Output:

[[23.  45.  43.  23.  21. ]
 [45.6 32.5 45.6  6.7  8.9]]

Now, we will convert this into pandas dataframes of float and integer types and integer type. We can do this by using the dtype parameter.

  • To convert to float – use dtype=’float’
  • To convert to integer – use dtype=’int’

Let’s see the code

#import pandas
import pandas as pd

#convert the numpy array to pandas dataframe with integer type
data=pd.DataFrame(  array,
                    columns=['col1','col2','col3','col4','col5'],
                    index=['row1','row2'],
                    dtype='int')

#display
print(data)

#convert the numpy array to pandas dataframe with float type
data=pd.DataFrame(  array,
                    columns=['col1','col2','col3','col4','col5'],
                    index=['row1','row2'],
                    dtype='float')

#display
print(data)

Output:

      col1  col2  col3  col4  col5
row1    23    45    43    23    21
row2    45    32    45     6     8


      col1  col2  col3  col4  col5
row1  23.0  45.0  43.0  23.0  21.0
row2  45.6  32.5  45.6   6.7   8.9

Summary

This article discussed five approaches for converting numpy array to pandas DataFrame using pandas.DataFrame() with examples.

Advertisements

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top