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:- If the values are callable, then pandas computes them and assigns them to the new columns.
- 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 pddf = 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
percentageusing theassign()method. In the method, we calculate the percentage of every student (with the help of alambdafunction) by dividing themarksby100. The new DataFrame returned is callednew_df. - Line 10: We print
new_dfto the console.
Code 2
import pandas as pddf = 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
universityusing theassign()method. In the method, we use a non-callable list to create the new values for theuniversitycolumn. The new DataFrame returned is callednew_df. - Line 10: We print the
new_dfto the console.