• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Pandas Insert Columns into a DataFrame in Python

Pandas Insert Columns into a DataFrame in Python

Author: Aditya Raj
Last Updated: March 6, 2023

We use a pandas dataframe to store and manipulate tabular data in python. In this article, we will discuss how to insert a new column into the pandas dataframe using the insert() method.

Table of Contents
  1. The Pandas insert() Method
  2. Pandas Insert a Column at The Beginning of a DataFrame
  3. Insert Column at The End of a DataFrame in Python
  4. Pandas Insert Column at a Specific Index in a DataFrame
  5. Conclusion

The Pandas insert() Method

The insert() method is used to insert a column into a dataframe at a specific position. It has the following syntax.

DataFrame.insert(loc, column, value, allow_duplicates=_NoDefault.no_default)

Here,

  • The loc parameter takes the index at which the new column is inserted as its input argument.
  • The column parameter takes the column name as its input.
  • The value parameter takes a list or pandas series as values for the specified column.
  • The allow_duplicates parameter is used to decide if we can insert duplicate column names into the dataframe. By default, the insert() method raises a ValueError exception if the dataframe contains a column with the same name that we are trying to insert. If you want to insert duplicate column names into the pandas dataframe, you can set the allow_duplicates parameter to True.

Pandas Insert a Column at The Beginning of a DataFrame

To insert a column at the beginning of a dataframe, we can use the insert() method. Here, we will set the loc parameter to 0 so that the new column is inserted at the beginning. You can observe this in the following example.

import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
        {"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
        {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
        {"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
        {"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
        {"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The original dataframe is:")
print(df)
df.insert(0,"Name", ["Aditya","Joel", "Sam", "Chris", "Riya", "Anne"])
print("The mofified dataframe is:")
print(df)

Output:

The original dataframe is:
   Roll  Maths  Physics  Chemistry
0     1    100       80         90
1     2     80      100         90
2     3     90       80         70
3     4    100      100         90
4     5     90       90         80
5     6     80       70         70
The mofified dataframe is:
     Name  Roll  Maths  Physics  Chemistry
0  Aditya     1    100       80         90
1    Joel     2     80      100         90
2     Sam     3     90       80         70
3   Chris     4    100      100         90
4    Riya     5     90       90         80
5    Anne     6     80       70         70

In this example, we first converted a list of dictionaries to a dataframe using the DataFrame() function. Then, we inserted the Name column in the created dataframe at index 0 using the insert() method. For this, we passed the value 0 as the first input argument, the string "Name" as the second input argument and the list of values as the third input argument to the insert() method.

Insert Column at The End of a DataFrame in Python

To insert a column at the end of the dataframe, we can directly assign the column values to the column name in the dataframe as shown below.

import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
        {"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
        {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
        {"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
        {"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
        {"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The original dataframe is:")
print(df)
df["Name"]= ["Aditya","Joel", "Sam", "Chris", "Riya", "Anne"]
print("The mofified dataframe is:")
print(df)

Output:

The original dataframe is:
   Roll  Maths  Physics  Chemistry
0     1    100       80         90
1     2     80      100         90
2     3     90       80         70
3     4    100      100         90
4     5     90       90         80
5     6     80       70         70
The mofified dataframe is:
   Roll  Maths  Physics  Chemistry    Name
0     1    100       80         90  Aditya
1     2     80      100         90    Joel
2     3     90       80         70     Sam
3     4    100      100         90   Chris
4     5     90       90         80    Riya
5     6     80       70         70    Anne

In the above example, we have used the indexing operator to insert a new column at the end of a dataframe.

Instead of the above approach, we can also use the insert() method to insert a column at the end. For this, we will use the following steps.

  • First, will obtain the list of column names using the columns attribute of the dataframe. The columns attribute contains a list of column names.
  • Next, we will use the len() function to find the total number of columns in the dataframe. Let it be numCol.
  • Once, we get the number of columns in the dataframe, we know that the current columns exist at the positions 0 to numCol-1. Hence, we will insert the new column to the dataframe at the index numCol using the insert() method.

After execution of the above steps, we can insert a column at the end of the dataframe as shown in the following example.

import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
        {"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
        {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
        {"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
        {"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
        {"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The original dataframe is:")
print(df)
numCol=len(df.columns)
df.insert(numCol,"Name", ["Aditya","Joel", "Sam", "Chris", "Riya", "Anne"])
print("The mofified dataframe is:")
print(df)

Output:

The original dataframe is:
   Roll  Maths  Physics  Chemistry
0     1    100       80         90
1     2     80      100         90
2     3     90       80         70
3     4    100      100         90
4     5     90       90         80
5     6     80       70         70
The mofified dataframe is:
   Roll  Maths  Physics  Chemistry    Name
0     1    100       80         90  Aditya
1     2     80      100         90    Joel
2     3     90       80         70     Sam
3     4    100      100         90   Chris
4     5     90       90         80    Riya
5     6     80       70         70    Anne

Pandas Insert Column at a Specific Index in a DataFrame

To insert a column at a specific position in the dataframe, you can use the insert() method as shown below.

import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
        {"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
        {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
        {"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
        {"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
        {"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The original dataframe is:")
print(df)
df.insert(2,"Name", ["Aditya","Joel", "Sam", "Chris", "Riya", "Anne"])
print("The mofified dataframe is:")
print(df)

Output:

The original dataframe is:
   Roll  Maths  Physics  Chemistry
0     1    100       80         90
1     2     80      100         90
2     3     90       80         70
3     4    100      100         90
4     5     90       90         80
5     6     80       70         70
The mofified dataframe is:
   Roll  Maths    Name  Physics  Chemistry
0     1    100  Aditya       80         90
1     2     80    Joel      100         90
2     3     90     Sam       80         70
3     4    100   Chris      100         90
4     5     90    Riya       90         80
5     6     80    Anne       70         70

In this example, we have inserted the Name column at index 2 of the input dataframe using the insert() method.

Conclusion

In this article, we discussed different ways to insert a column in a pandas dataframe. To learn more about python programming, you can read this article on how to create an empty dataframe in python. You might also like this article on working with XML files in Python.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us