Add multiple columns to a data frame using Dataframe.insert() method

Using DataFrame.insert() method, we can add new columns at specific position of the column name sequence. Although insert takes single column name, value as input, but we can use it repeatedly to add multiple columns to the DataFrame. 

Python3




# importing pandas library
import pandas as pd
 
# creating and initializing a nested list
students = [['jackma', 34, 'Sydeny', 'Australia'],
            ['Ritika', 30, 'Delhi', 'India'],
            ['Vansh', 31, 'Delhi', 'India'],
            ['Nany', 32, 'Tokyo', 'Japan'],
            ['May', 16, 'New York', 'US'],
            ['Michael', 17, 'las vegas', 'US']]
 
# Create a DataFrame object
df = pd.DataFrame(students,
                  columns=['Name', 'Age', 'City', 'Country'],
                  index=['a', 'b', 'c', 'd', 'e', 'f'])
 
# creating columns 'Age' and 'ID' at
# 2nd and 3rd position using
# dataframe.insert() function
df.insert(2, "Marks", [90, 70, 45, 33, 88, 77], True)
df.insert(3, "ID", [101, 201, 401, 303, 202, 111], True)
 
 
# Displaying the Data frame
df


Output :

Added multiple columns using DataFrame insert() Method

Add multiple columns to dataframe in Pandas

In Pandas, we have the freedom to add columns in the data frame whenever needed. There are multiple ways to add columns to pandas dataframe. 

Similar Reads

Add multiple columns to a DataFrame using Lists

Python3 # importing pandas library import pandas as pd   # creating and initializing a nested list students = [['jackma', 34, 'Sydeny', 'Australia'],             ['Ritika', 30, 'Delhi', 'India'],             ['Vansh', 31, 'Delhi', 'India'],             ['Nany', 32, 'Tokyo', 'Japan'],             ['May', 16, 'New York', 'US'],             ['Michael', 17, 'las vegas', 'US']]   # Create a DataFrame object df = pd.DataFrame(students,                   columns=['Name', 'Age', 'City', 'Country'],                   index=['a', 'b', 'c', 'd', 'e', 'f'])   # Creating 2 lists 'marks' and 'gender' marks = [85.4,94.9,55.2,100.0,40.5,33.5] gender = ['M','F','M','F','F','M']   # adding lists as new column to dataframe df df['Uni_Marks'] = marks df['Gender'] = gender   # Displaying the Data frame df...

Add multiple columns to a data frame using Dataframe.assign() method

...

Add multiple columns to a data frame using Dataframe.insert() method

Using DataFrame.assign() method, we can set column names as parameters and pass values as list to replace/create the columns....

Add multiple columns to a data frame using Dictionary and zip()

...