• 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 Assign New Columns to a DataFrame

Pandas Assign New Columns to a DataFrame

Author: Aditya Raj
Last Updated: March 8, 2023

Pandas dataframes are the data structures that we use to handle tabular data in python. This article discusses different ways to assign new columns to pandas dataframe using the assign() method.

Table of Contents
  1. The Pandas assign() Method
  2. Pandas Assign a Column to a DataFrame
  3. Assign a Column Based on Another Column
  4. Assign Multiple Columns to a DataFrame
  5. Conclusion

The Pandas assign() Method

The assign() method is used to assign new columns to a pandas dataframe. It has the following syntax.

df.assign(col_1=series_1, col_2=series2,...)

In the above function, the column names and the values for the columns are passed as keyword arguments. Here, column names are the keywords, and list or series objects containing the data for the column are the corresponding values.

When we invoke the assign() method on a dataframe df, it takes column names and series objects as its input argument. After execution, the assign() method adds the specified column to the dataframe df and returns the modified dataframe. 

Pandas Assign a Column to a DataFrame

To assign a new column to the pandas dataframe using the assign() method, we will pass the column name as its input argument and the values in the column as the value assigned to the column name parameter.

After execution, the assign() method returns the modified dataframe. 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=df.assign(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 created a column "Name" in the input dataframe using a list and the assign() method.

Instead of a list, we can also assign a pandas series to the column name parameter to create a new column 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=df.assign(Name= pd.Series(["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 this example, we passed a series to the assign() method as an input argument instead of a list. However, you can observe that the output dataframe in this example is the same as the previous example.

Assign a Column Based on Another Column

We can also create a column based on another column in a pandas dataframe. For this, we will first create a series based on another column. Then, we can use the assign() method and pass the column name with the series as its input to assign the column to the pandas dataframe.

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=df.assign(GPI= df["Maths"]/10)
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   GPI
0     1    100       80         90  10.0
1     2     80      100         90   8.0
2     3     90       80         70   9.0
3     4    100      100         90  10.0
4     5     90       90         80   9.0
5     6     80       70         70   8.0

In this example, we have created the GPI column using the "Maths" column. For this, we created a series by dividing the Maths column by 10. Then, we assigned the new series to the GPI keyword as an input argument in the assign() method. You can also use the pandas apply method to create a new series in this case.

Assign Multiple Columns to a DataFrame

To assign multiple columns to the pandas dataframe, you can use the assign() 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=df.assign(Name=["Aditya","Joel", "Sam", "Chris", "Riya", "Anne"],GPI= df["Maths"]/10)
print("The mofified dataframe is:")
print(df)

Output:

he 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   GPI
0     1    100       80         90  Aditya  10.0
1     2     80      100         90    Joel   8.0
2     3     90       80         70     Sam   9.0
3     4    100      100         90   Chris  10.0
4     5     90       90         80    Riya   9.0
5     6     80       70         70    Anne   8.0

In this example, we assigned two columns to the pandas dataframe using the assign() method. For this, we passed both the column names and their values as keyword arguments to the assign() method.

Conclusion

In this article, we have discussed different ways to assign columns to a pandas dataframe. To learn more about python programming, you can read this article on how to use the insert() method to insert a column into a dataframe. You might also like this article on how to convert epoch to datetime 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 bitly 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 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 Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Pandas Append Row to DataFrame
  • Convert String to DataFrame in Python
  • Pandas DataFrame to List in Python
  • Solved: Dataframe Constructor Not Properly Called Error in Pandas
  • Overwrite a File in Python

Copyright © 2012–2023 · PythonForBeginners.com

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