Add numpy array to Pandas Dataframe as column

In this post, we are going to understand how to Add a numpy array to Pandas Dataframe as a column with examples. To store a numpy array into the cell of the dataframe, we will pass the name of the cell in square brackets[] and assign a numpy array to this cell. To add rows to dataframe

1. Add numpy array to Pandas Dataframe as column


In this below Python program, we have a numpy array of values [‘A’, ‘B’, ‘C’] that we are adding to the existing dataframe simply passing the name of the column inside the square brackets [‘Letter’] and assigning numpy array to it.

The python pandas library must be installed on the system to run the below program. We can install pandas by using the command “pip install pandas”.

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', 'Letter'])

dfobj['Letter']= np.array(['A','B','C']).tolist()

print(dfobj)

Output

   Name Age Weight Letter
0   Tom   9      8      A
1  Trex   6     15      B
2  Kity   7     11      C

2. Append 2D numpy array to Pandas Dataframe as column


In this Python program example, We will understand how to add a 2D numpy array to pandas dataframe as a column. We are converting a list of lists to pandas series add assigning it to the column of the dataframe.

import numpy as np
import pandas as pd

df = pd.DataFrame(np.arange(1,10).reshape(3,3))
df['Column2'] = pd.Series(np.array([[2, 3, 4],
[7, 8, 9],
[10, 14, 16]]))

print(df)

Output

  0  1  2       Column2
0  1  2  3     [2, 3, 4]
1  4  5  6     [7, 8, 9]
2  7  8  9  [10, 14, 16]

3. Append 2D numpy array to existing dataframe


In this python program example, we are adding a 2D numpy array to the pandas dataframe. We are converting the numpy array to a list and adding it to the dataframe new column ‘Letter’

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', 'Letter'])

dfobj['Letter']= np.array([['A','B','C'],['D','E','F'],['G','H','I']]).tolist()

print(dfobj)


Output

   Name Age Weight     Letter
0   Tom   9      8  [A, B, C]
1  Trex   6     15  [D, E, F]
2  Kity   7     11  [G, H, I]

4. Store NumPy array in cell of Pandas Dataframe


In this python program, we are storing a numpy array in the cell of pandas dataframe by using apply() and lambda function as shown below example this way we add numpy array to pandas 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)


dfobj['MynewCol'] = dfobj.apply(lambda r: tuple(r), axis=1).apply(np.array)


print(dfobj)

Output

   0  1   2   3           MynewCol
0   Tom  9   8  WA    [Tom, 9, 8, WA]
1  Trex  6  15  CA  [Trex, 6, 15, CA]
2  Kity  7  11  WA  [Kity, 7, 11, WA]

5.Append Matric as new column to dataframe


In this example, we will learn how to append a matric to the dataframe.In this below code example, We are appending new columns (cost, Time) to the existing dataframe.

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

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

newarr = np.array(np.arange(6).reshape(3, 2))


dfobj = pd.concat([dfobj, pd.DataFrame(newarr, columns=["Cost", "Time"])], axis=1)



print(dfobj)

Output

   Name Age Weight Letter  Cost  Time
0   Tom   9      8     WA     0     1
1  Trex   6     15     CA     2     3
2  Kity   7     11     WA     4     5

6. 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

Summary

In this post we have learned how to Add numpy array to Pandas Dataframe as column with examples.