pandas
  1. pandas-dataframeassign

DataFrame.assign() - Pandas DataFrame Basics

The assign() method is one of the many useful methods in Pandas for working with data frames. In this tutorial, we will discuss the assign() method and its use in creating new columns in a data frame.

Syntax

The basic syntax of the assign() method is as follows:

df.assign(col_name=value)

Here, df is the data frame, col_name is the name of the new column, and value is the value or the calculated value for the column.

Example

Consider the following example where a data frame is created with three columns Name, Age, and Gender. The assign() method is then used to create a new column City based on a dictionary of city names.

import pandas as pd

data = {'Name': ['John', 'Sara', 'Ravi'],
        'Age': [25, 30, 35],
        'Gender': ['M', 'F', 'M']}

df = pd.DataFrame(data)

df = df.assign(City={'John': 'New York', 'Sara': 'London', 'Ravi': 'Mumbai'})

print(df)

Output

   Name  Age Gender      City
0  John   25      M  New York
1  Sara   30      F    London
2  Ravi   35      M    Mumbai

In this example, we create a data frame with three columns Name, Age, and Gender. We then create a new column City using the assign() method and a dictionary of city names, where the keys are the names in the Name column and the values are the corresponding city names.

Explanation

The assign() method is used to create a new column in a data frame based on a given value or calculation. In the above example, we use a dictionary of city names to create a new column City with the corresponding city names based on the names in the Name column of the data frame.

The assign() method returns a new data frame with the new column added and the original data frame unchanged.

Use

The assign() method is a useful tool for creating new columns in a Pandas data frame based on a given value or calculation. It is particularly useful when performing data analysis and data transformation tasks.

Important Points

  • The assign() method creates a new data frame with the new column added and the original data frame unchanged.
  • The new column name and its value are specified as arguments to the assign() method.
  • Multiple new columns can be created at once using assign() method.

Summary

In this tutorial, we discussed the assign() method in Pandas and its use in creating new columns in a data frame. The method is straightforward to use and offers a quick way to add new columns based on calculated values or given values. The assign() method can be particularly useful in data analysis and transformation tasks using Pandas.

Published on: