Add multiple columns to dataframe in Pandas - GeeksforGeeks

01 Aug, 2020
In Pandas, we have the freedom to add columns in the data frame whenever needed. There are multiple ways to add columns to the Pandas data frame. 
Method 1: Add multiple columns to a data frame using Lists
Python3
# importing pandas libraryimport pandas as pd  # creating and initializing a nested liststudents = [['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 objectdf = 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 dfdf['Uni_Marks'] = marksdf['Gender'] = gender  # Displaying the Data framedf
Output :
Method 2: Add multiple columns to a data frame using  Dataframe.assign() method
Python3
# importing pandas libraryimport pandas as pd  # creating and initializing a nested liststudents = [['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 objectdf = pd.DataFrame(students,                  columns=['Name', 'Age', 'City', 'Country'],                  index=['a', 'b', 'c', 'd', 'e', 'f'])  # creating columns 'Admissionnum' and 'Percentage'# using dataframe.assign() functiondf = df.assign(Admissionnum=[250, 800, 1200, 300, 400, 700],                Percentage=['85%', '90%', '75%', '35%', '60%', '80%'])  # Displaying the Data framedf
Output :
Method 3: Add multiple columns to a data frame using  Dataframe.insert() method
Python3
# importing pandas libraryimport pandas as pd  # creating and initializing a nested liststudents = [['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 objectdf = 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() functiondf.insert(2, "Marks", [90, 70, 45, 33, 88, 77], True)df.insert(3, "ID", [101, 201, 401, 303, 202, 111], True)    # Displaying the Data framedf
Output :
Method 4: Add multiple columns to a data frame using  Dictionary and zip()
Python3
# importing pandas libraryimport pandas as pd  # creating and initializing a nested liststudents = [['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 objectdf = pd.DataFrame(students,                  columns=['Name', 'Age', 'City', 'Country'],                  index=['a', 'b', 'c', 'd', 'e', 'f'])  # creating 2 lists 'ids' and 'marks'ids = [11, 12, 13, 14, 15, 16]marks=[85,41,77,57,20,95,96]  # Creating columns 'ID' and 'Uni_marks'  # using Dictionary and zip() df['ID'] = dict(zip(ids, df['Name']))df['Uni_Marks'] = dict(zip(marks, df['Name']))    # Displaying the Data framedf
Output :
Python pandas-dataFrame
Python Pandas-exercise
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python
Defaultdict in Python
Python | Pandas dataframe.groupby()
Python | Get unique values from a list