• 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 / Insert New Column Into a Dataframe in Python

Insert New Column Into a Dataframe in Python

Author: Aditya Raj
Last Updated: June 25, 2022

Dataframes are often used to handle tabular data in python. In this article, we will discuss how we can insert a new column into a dataframe in python.

Insert New Column Into a Dataframe by Indexing in Python

To add a new column into a dataframe, we can use indexing in the same way we add a key-value pair in a python dictionary. In this approach, we will first put all the elements of the column that needs to be inserted into a list. After that, we will add the list as a new column into the dataframe using the following syntax.

datframe_name[column_name]= element_list

Here, 

  • datframe_name is the name of the dataframe into which the column has to be inserted.
  • column_name represents an string that contains the name of the new column.
  • element_list  denotes list containing the elements that will be inserted to the dataframe.

Following is the python source code to insert a new column into a dataframe by indexing.

import pandas as pd

df = pd.read_csv('Demo.csv')
print("The dataframe before inserting the column:")
print(df)
column_data = [180, 164, 170]
df['Height'] = column_data
print("The dataframe after inserting the column:")
print(df)

Output:

The dataframe before inserting the column:
   Roll    Name Language
0     1  Aditya   Python
1     2     Sam     Java
2     3   Chris      C++
The dataframe after inserting the column:
   Roll    Name Language  Height
0     1  Aditya   Python     180
1     2     Sam     Java     164
2     3   Chris      C++     170

Insert New Column Into a Dataframe Using the assign() Method

Instead of using indexing, we can use the assign() method to add a new column into the dataframe. The assign() method, when invoked on a dataframe, takes the column name and the list of elements of the new column as a keyword argument using the following syntax.

datframe_name.assign(column_name= element_list)

Here,

  • datframe_name is the name of the dataframe into which the column has to be inserted.
  • column_name denotes the name of the new column.
  • element_list  is the list containing the elements that will be inserted to the dataframe.

After execution, the assign() method inserts the column into the dataframe and returns the updated dataframe as shown below.

import pandas as pd

df = pd.read_csv('Demo.csv')
print("The dataframe before inserting the column:")
print(df)
column_data = [180, 164, 170]
df = df.assign(Height=column_data)
print("The dataframe after inserting the column:")
print(df)

Output:

The dataframe before inserting the column:
   Roll    Name Language
0     1  Aditya   Python
1     2     Sam     Java
2     3   Chris      C++
The dataframe after inserting the column:
   Roll    Name Language  Height
0     1  Aditya   Python     180
1     2     Sam     Java     164
2     3   Chris      C++     170

The above approaches are used to insert the new column at the end. We can also insert new columns at any position in the dataframe. For that, we can use the insert() method.

Insert New Column Into a Dataframe Using the insert() Method

With the insert() method, we can insert a new column at any position in the dataframe. The insert() method, when invoked on a dataframe, takes the position at which the new column will be inserted as its first input argument, the name of the new column as the second input argument, the list containing the elements of the new column as the third input argument. After execution, it inserts the column at the specified position in the dataframe. You can observe this in the following example.

import pandas as pd

df = pd.read_csv('Demo.csv')
print("The dataframe before inserting the column:")
print(df)
column_data = [180, 164, 170]
df.insert(1, 'Height', column_data)
print("The dataframe after inserting the column:")
print(df)

Output:

The dataframe before inserting the column:
   Roll    Name Language
0     1  Aditya   Python
1     2     Sam     Java
2     3   Chris      C++
The dataframe after inserting the column:
   Roll  Height    Name Language
0     1     180  Aditya   Python
1     2     164     Sam     Java
2     3     170   Chris      C++

Conclusion

In this article, we have discussed three approaches to insert a new column into a dataframe in python. To know more about python programming, you can read this article on list comprehension in python. You might also like this article on dictionary comprehension in python.

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