Pandas DataFrame assign() Method
Example
Add a new column to the DataFrame:
import pandas as pd
data = {
"age": [16, 14, 10],
"qualified": [True, True, True]
}
df = pd.DataFrame(data)
newdf
= df.assign(name = ["Emil", "Tobias", "Linus"])
Try it Yourself »
Definition and Usage
The assign()
method adds a new column to an
existing DataFrame.
Syntax
dataframe.assign(kwargs)
Parameters
Parameter | Description |
---|---|
kwargs | Required. The new column name(s) with values |
Return Value
A DataFrame with the new column(s) added.
This method does not change the original DataFrame.
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.