Numpy Array to Pandas DataFrame

In this post, we are going to understand how to convert Numpy Array to Pandas DataFrame with different examples. The Pandas Dataframe contains data in table form. The Pandas. dataframe() is used to create a pandas dataframe from a numpy array. we make sure numpy and pandas are installed on the system

1.Numpy Array to Pandas DataFrame


In this example, we will understand how to convert the Numpy array to a dataframe. We have a numpy array that we will pass as an argument to PD.dataframe() to create a dataframe.

  • Import both module using code
    • import numpy as np
    • import pandas as pd
  • Using pd.dataframe() to create dataframe from numpy array.
  • After conversion we are verify daty type by using type() function
import numpy as np
import pandas as pd

myarr = np.array([ ['Tom', 9, 8, 'WA'],  ['Trex', 6, 15, 'CA'],  ['Kity', 7, 11, 'WA']])

dfobj = pd.DataFrame(myarr)

print(dfobj)
print(type(dfobj))

Output

    0  1   2   3
0   Tom  9   8  WA
1  Trex  6  15  CA
2  Kity  7  11  WA
<class 'pandas.core.frame.DataFrame'>

2. NumPy array to pandas dataframe with column name


In this python program, we will understand how to convert numpy array to pandas dataframe with column name/header

  • We create dataframe with column name by passing a list of columns to dataframe parameters ‘columns’ that defult value for columns is RangeIndex[0,1,2,3….n].We can specify our own label to column parameter is as give below
import numpy as np
import pandas as pd

myarr = np.array([ ['Tom', 9, 8, 'WA'],  ['Trex', 6, 15, 'CA'],  ['Kity', 7, 11, 'WA']])

dfobj = pd.DataFrame(myarr, columns = ['Name', 'Age', 'Weight', 'City'])

print(dfobj)

Output

 Name  Age  Weight City
0   Tom    9       8   WA
1  Trex    6      15   CA
2  Kity    7      11   WA

3. 2D NumPy array to Pandas DataFrame


In this example, we are converting a 2D numpy array to pandas dataframe along with columns name by passing numpy array to dataframe.

import numpy as np
import pandas as pd

myarr = np.array([ ['Tom', 9, 8, 'WA'],  ['Trex', 6, 15, 'CA'],  ['Kity', 7, 11, 'WA']])

dfobj = pd.DataFrame(myarr, columns = ['Name', 'Age', 'Weight', 'City'])

print(dfobj)

Output

 Name  Age  Weight City
0   Tom    9       8   WA
1  Trex    6      15   CA
2  Kity    7      11   WA

4. 1D Numpy array to Pandas DataFrame


In this example, We are converting a 1D numpy array to a pandas dataframe and splitting it into multiple columns. The given numpy array is reshaped using numpy.reshape() function.

import numpy as np
import pandas as pd

myarr = np.array(['Tom', 9, 8, 'WA'])

dfobj = pd.DataFrame(myarr.reshape(-1, len(myarr)),columns = ['Name', 'Age', 'Weight', 'City'])                  

print(dfobj)

Output

 Name Age Weight City
0  Tom   9      8   WA

5. Append Numpy array as a row to DataFrame


Sometimes, we have to append a numpy array to the existing dataframe as a row that can be simply achieved by using the dataframe.append() method.

  • We have to numpy myarr that used to create a dataframe.
  • arrtoappend that we have to append as a row to pandas dataframe.
  • We have created a dataframe by using pandas.dataframe() method by passing myarr numpy array as agrument.
  • The dataframe append method to append numpy array as row.
import numpy as np
import pandas as pd

myarr = np.array([ ['Tom', 9, 8, 'WA'],  ['Trex', 6, 15, 'CA'],  ['Kity', 7, 11, 'WA']])

arrToappend = np.array([['Dog', 9, 8, 'WA']])

dfobj = pd.DataFrame(myarr, columns = ['Name', 'Age', 'Weight', 'City'])

print('Dataframe before append:\n',dfobj)


dfobj = dfobj.append(pd.DataFrame( arrToappend,
               columns=[ 'Name', 'Age', 'Weight', 'City']),
               ignore_index = True)


print('\nAfter append:\n',dfobj)



Output

Dataframe before append:
    Name Age Weight City
0   Tom   9      8   WA
1  Trex   6     15   CA
2  Kity   7     11   WA

After append:
    Name Age Weight City
0   Tom   9      8   WA
1  Trex   6     15   CA
2  Kity   7     11   WA
3   Dog   9      8   WA

6.Numpy 3d array to Pandas dataframe


In this example, we are converting a 3D Numpy array to a pandas dataframe, Simply passing the 3D numpy array to pandas.dataframe() function as parameter.

import numpy as np
import pandas as pd

myarr = np.array([ [['Tom', 9],  ['Trex', 6],  ['Kity', 7]],[['Dog', 9],['Cat', 3],['Rat', 1]],
                   [['Bee', 0],['Hen', 2],['Duck', 1]]])




m,n,r = myarr.shape
outarr = np.column_stack((np.repeat(np.arange(m),n),myarr.reshape(m*n,-1)))


dfobj = pd.DataFrame(outarr, columns = ['No', 'Age', 'Weight'])


print(dfobj)


Output

  No   Age Weight
0  0   Tom      9
1  0  Trex      6
2  0  Kity      7
3  1   Dog      9
4  1   Cat      3
5  1   Rat      1
6  2   Bee      0
7  2   Hen      2
8  2  Duck      1

7. Numpy array to dataframe with index


Sometimes we have to convert numpy array to pandas dataframe with index. The pandas. dataframe() method index parameter is used to pass the custom indexes label to the pandas dataframe. The default value for index is rangindex(0,1,2….)

import numpy as np
import pandas as pd


myarr = np.array([ ['Tom', 9, 8, 'WA'],  ['Trex', 6, 15, 'CA'],  ['Kity', 7, 11, 'WA']])

dfobj = pd.DataFrame(myarr, columns = ['Name', 'Age', 'Weight', 'City'],index=['Row_1','Row_2','Row_3'])

print(dfobj)

Output

       Name Age Weight City
Row_1   Tom   9      8   WA
Row_2  Trex   6     15   CA
Row_3  Kity   7     11   WA

8. NumPy to dataframe without index


In this example, we are converting numpy array to pandas dataframe without index, First, we have created pandas dataframe by using pd.dataframe() and later set index off by using to_string(index=False)

import numpy as np
import pandas as pd


myarr = np.array([ ['Tom', 9, 8, 'WA'],  ['Trex', 6, 15, 'CA'],  ['Kity', 7, 11, 'WA']])

dfobj = pd.DataFrame(myarr, columns = ['Name', 'Age', 'Weight', 'City'])

print(dfobj.to_string(index=False))

Output

 Name Age Weight City
  Tom   9      8   WA
 Trex   6     15   CA
 Kity   7     11   WA

Consclusion

In this post, we have understood 8 ways of Numpy Array to Pandas DataFrame with examples