Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Dataframe.assign() method assign new columns to a DataFrame, returning a new object (a copy) with the new columns added to the original ones. Existing columns that are re-assigned will be overwritten.
Length of newly assigned column must match the number of rows in the dataframe.
Syntax: DataFrame.assign(**kwargs)
Parameters:
kwargs : keywords are the column names. If the values are callable, they are computed on the DataFrame and assigned to the new columns. The callable must not change input DataFrame (though pandas don’t check it). If the values are not callable, (e.g. a Series, scalar, or array), they are simply assigned.
Returns: A new DataFrame with the new columns in addition to all the existing columns.
For link to CSV file Used in Code, click here
Example #1: Assign a new column called Revised_Salary with 10% increment of the original Salary.
import pandas as pd
df = pd.read_csv("nba.csv")
df[:10]
|

df.assign(Revised_Salary = lambda x: df['Salary']
+ df['Salary']/10)
|
Output:

Example #2: Assigning more than one column at a time
import pandas as pd
df = pd.read_csv("nba.csv")
df.assign(New_team = lambda x: df['Team']+'_GO',
Revised_Salary = lambda x: df['Salary']
+ df['Salary'] / 10)
|
Output:

Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the
Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
16 Nov, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...