What is the assign method in pandas?

Overview

We use the assign() method to add additional columns to the existing ones in a DataFrame. This method returns a new DataFrame object. Reassigned columns will overwrite any previously assigned columns.

Note: Click here to learn more about the pandas library.

Syntax

DataFrame.assign(**kwargs)

Parameters

  • **kwargs: This indicates keyword arguments where the column names are keywords. The values of the keys can be one of the following:
    1. If the values are callable, then pandas computes them and assigns them to the new columns.
    2. They are assigned if the values aren’t callable (for example, a series, scalar, or array).

Return value

The method returns a new DataFrame with the new columns besides all the existing columns.

Code 1

import pandas as pd
df = pd.DataFrame({"student_name":["john","Sam","tom"],"marks":[80,45,99]})
print(df)
print(":::::::New DataFrame:::::::")
new_df = df.assign(percentage=lambda x: x.marks / 100)
print(new_df)

Explanation

  • Line 8: We create a new column called percentage using the assign() method. In the method, we calculate the percentage of every student (with the help of a lambda function) by dividing the marks by 100. The new DataFrame returned is called new_df.
  • Line 10: We print new_df to the console.

Code 2

import pandas as pd
df = pd.DataFrame({"student_name":["john","Sam","tom"],"marks":[80,45,99]})
print(df)
print(":::::::New DataFrame:::::::")
new_df = df.assign(university=["MIT","Harvard","USC"])
print(new_df)

Explanation

  • Line 8: We create a new column called university using the assign() method. In the method, we use a non-callable list to create the new values for the university column. The new DataFrame returned is called new_df.
  • Line 10: We print the new_df to the console.

Free Resources