Pandas DataFrame assign() Method | Create new Columns in DataFrame
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, making importing and analyzing data much easier.
The Dataframe.assign() method assigns 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. The length of the newly assigned column must match the number of rows in the DataFrame.
Example:
Python3
import numpy as np
arr = np.array([ 1 , 2 , 3 , 4 , 5 ])
print ( "Original Array:" , arr)
arr.assign([ 6 , 7 , 8 , 9 , 10 ])
print ( "Modified Array:" , arr)
|
Output:
Original Array: [1 2 3 4 5]
Modified Array: [ 6 7 8 9 10]
Syntax
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 the link to the CSV file Used in the Code, click here
Examples
Let’s look at some Python programs and learn how to use the assign() method of the Pandas library to create new columns in DataFrame with these examples.
Example 1:
Assign a new column called Revised_Salary with a 10% increment of the original Salary.
Python3
import pandas as pd
df = pd.read_csv( "nba.csv" )
df[: 10 ]
|
Python3
df.assign(Revised_Salary = lambda x: df[ 'Salary' ]
+ df[ 'Salary' ] / 10 )
|
Output:
Assigning more than one column at a time
Python3
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:
Last Updated :
09 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...