Add mutiple columns to Pandas DataFrame

In this example, we are going to learn how to Add mutiple columns to Pandas DataFrame by using assign(), insert(),[] operator, and using a dictionary.

1. Add mutiple columns to Pandas dataframe using [] operator


In this example, we are adding new ‘City’, ‘Food‘ columns using the [] operator in the dataframe.To add columns to DataFrame Using [] operator. we pass column name between [] operator and assign a list of column values the code for this is dfobj[‘city’] = [‘WA’,‘CA’,’NY’] and dfobj[‘Food’] = [‘A’,’B’,’C’]

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

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



City = ['WA','CA','WA']
Food = ['A','B','C']  

dfobj['City'] = City
dfobj['Food'] = Food

print(dfobj)

Output

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

2. Assign() method to Add mutiple columns to Pandas Dataframe


The assign() function assigns a new column to the dataframe and returns a new object with all the existing columns in addition to the new one. if existing columns is re-assign will be overwritten.

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

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



City = ['WA','CA','WA']
Food = ['A','B','C']  



dfobj = dfobj.assign(City=City, 
               Food=Food)

print(dfobj)


Output

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

3. insert() method add mutiple columns to Pandas Datafame


Sometimes we want to add multiple columns at the specific index. In this example we are adding columns at index 1,2 indexes The insert() method at a specific index in pandas dataframe.

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

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



City = ['WA','CA','WA']
Food = ['A','B','C']  


dfobj.insert(1, "City", City, True)
dfobj.insert(2, "Food", Food, True)

print(dfobj)

Output

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

4. Add multiple columns by a dictionary in dataframe


In this example, we are adding columns City, Food by using the dict() and zip() method. The list of city, food values is the zip to the existing column ‘Name’ to create a dictionary.

import pandas as pd
import numpy as np
  
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':[100,98,99],
    'Subject': ['Math','Math','Math']
}
  
dfobj = pd.DataFrame(Student_dict)
 
citys = ['WA', 'CA','NY']
Foods = ['A','B','C']
 
dfobj['City'] = dict(zip(citys, dfobj['Name']))
dfobj['Food'] = dict(zip(citys, dfobj['Name']))
print(dfobj)

Output

   Name  Marks Subject City Food
0  Jack    100    Math   WA   WA
1  Rack     98    Math   CA   CA
2   Max     99    Math   NY   NY

5. Add multiple columns with same values


In this Python program example, we are adding multiple columns ‘City’, ‘Food’ with the same values. Let us understand with examples.

import pandas as pd
import numpy as np
  
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':[100,98,99],
    'Subject': ['Math','Math','Math']
}
  
dfobj = pd.DataFrame(Student_dict)
 
 
 
dfobj['Ctty'] = 'WA'
dfobj['Food'] ='A'
print(dfobj)

Output

   Name  Marks Subject Ctty Food
0  Jack    100    Math   WA    A
1  Rack     98    Math   WA    A
2   Max     99    Math   WA    A

Summary

In this post how to Add mutiple columns to Pandas DataFrame by using the panda’s library function assign(),[]operator,insert() and using dictionary.