How To Convert Numpy Array To Pandas Dataframe – With Examples?

Numpy arrays are used for array computing. It can be used for performing a number of mathematical operations such as algebraic, trigonometric, and statistical routines.

You can convert the NumPy array to Pandas Dataframe by using the pd.DataFrame(array) method.

If you’re in Hurry

You can use the following code to convert the NumPy array to Pandas Dataframe.

Snippet

import numpy as np
import pandas as pd

array = np.random.rand(5, 5)

df = pd.DataFrame(array)

df

This is how you can create a pandas dataframe from the NumPy Array.

If You Want to Understand Details, Read on…

In this tutorial, you’ll learn the different methods available to create pandas dataframe from the NumPy Array.

Creating NumPy Array

First, you’ll create a NumPy array which will be converted to pandas Dataframe.

You can create a NumPy array by using the np.random.rand() method. This will create a 5 X 5-dimensional array filled with random values.

Snippet

import numpy as np
import pandas as pd

array = np.random.rand(5, 5)

array

When you print the array, you’ll see the output of 5 rows and 5 columns with random values.

Output

    array([[0.93083461, 0.49167774, 0.43159395, 0.4410153 , 0.80704423],
           [0.92919269, 0.58450733, 0.6947164 , 0.6369035 , 0.31362118],
           [0.53760608, 0.83053222, 0.3622226 , 0.57997871, 0.83459934],
           [0.70689251, 0.32799213, 0.01533952, 0.0212185 , 0.93386042],
           [0.13681433, 0.90448399, 0.67102222, 0.45538514, 0.15043999]])

Convert Numpy Array to Pandas Dataframe

In this section, you’ll learn how to convert Numpy array to pandas dataframe without using any additional options such as column names or indexes.

You can convert the NumPy array to a pandas dataframe using the dataframe constructor pd.DataFrame(array).

Code

df = pd.DataFrame(array)

df

When you print the dataframe using df, you’ll see the array is converted as a dataframe.

DataFrame will look Like

01234
00.9308350.4916780.4315940.4410150.807044
10.9291930.5845070.6947160.6369040.313621
20.5376060.8305320.3622230.5799790.834599
30.7068930.3279920.0153400.0212190.933860
40.1368140.9044840.6710220.4553850.150440

This is how you can create a dataframe using the NumPy array without any additional options.

Convert NumPy Array to Pandas Dataframe with Column Names

Typically, NumPy arrays don’t have column names. Hence, while converting the NumPy arrays to Pandas dataframe, there will not be any column names assigned to the dataframe.

You can convert NumPy Array to pandas dataframe with column names using the attribute columns and passing the column values as a list.

Code

Use the following code to convert the NumPy array to a pandas dataframe with column names.

  • The list of column values must be in the same dimension as the array columns.
  • If you’ve 5 columns in the array, then you need to pass 5 values in the list.
df = pd.DataFrame(array, columns = ['Col_one', 'Col_two', 'Col_Three', 'Col_Four', 'Col_Five'])

df

When you print the dataframe using df, you’ll see that columns in the dataframe are named accordingly.

DataFrame will look Like

Col_oneCol_twoCol_ThreeCol_FourCol_Five
00.9308350.4916780.4315940.4410150.807044
10.9291930.5845070.6947160.6369040.313621
20.5376060.8305320.3622230.5799790.834599
30.7068930.3279920.0153400.0212190.933860
40.1368140.9044840.6710220.4553850.150440

This is how you can create a pandas dataframe using the NumPy array with column values.

Convert Numpy Array to Pandas Dataframe with Index

Typically, NumPy arrays don’t have row indexes. Hence, while converting the NumPy arrays to Pandas dataframe, there will not be any indexes assigned to the dataframe.

You can convert NumPy Array to pandas dataframe with index using the attribute index and passing the index values as a list.

Snippet

Use the following code to convert the NumPy array to a pandas dataframe with an index.

  • The list of index values must be in the same dimension as the array rows.
  • If you’ve 5 rows in the array, then you need to pass 5 values in the index list.
df = pd.DataFrame(array, columns = ['Col_one', 'Col_two', 'Col_Three', 'Col_Four', 'Col_Five'],  index = ['Row_1', 'Row_2','Row_3','Row_4','Row_5'])

df

DataFrame will look Like

Col_oneCol_twoCol_ThreeCol_FourCol_Five
Row_10.9308350.4916780.4315940.4410150.807044
Row_20.9291930.5845070.6947160.6369040.313621
Row_30.5376060.8305320.3622230.5799790.834599
Row_40.7068930.3279920.0153400.0212190.933860
Row_50.1368140.9044840.6710220.4553850.150440

This is how you can create a pandas dataframe with a NumPy array with index values.

Convert Object Type NumPy array to Dataframe

In this section, you’ll learn how to convert an object-type NumPy array, which has different types of data in each column, to a pandas dataframe.

First, create a NumPy.ndarray with String value in one column and int value in one column.

For example,

  • First column has country names which are of String type
  • Second column has a country codes which are of Int type.

Code

import numpy as np

arr = np.array([['India',1],['Germany',2],['US',3]], dtype=object)

print(arr)
print(type(arr))
print(arr.dtype)

Output

    [['India' 1]
     ['Germany' 2]
     ['US' 3]]
    <class 'numpy.ndarray'>
    object

Now, convert this ndarray into a dataframe object.

  • Use the DataFrame() constructor available in the pandas library to convert Numpy ndarray to a dataframe.
  • Optionally, pass the name for columns using the columns[] attribute as shown below.

Code

df = pd.DataFrame(arr, columns = ['Country', 'Code'])

df

DataFrame will look Like

CountryCode
0India1
1Germany2
2US3

You can check the type of the dataframe columns using the below snippet.

Snippet

df.dtypes

You can see both columns are created as objects rather than creating the code column as a number.

Output

Country       object
Code          object
dtype: object

If you want to convert the code column to a number, read the Change column type in Pandas tutorial.

You May Also Like

Leave a Comment