Assign New Columns to DataFrame

To assign new columns to a DataFrame, call assign() method on this DataFrame and pass new columns as parameters to this function.

In this tutorial, we will learn the syntax of DataFrame.assign() method and how to use this function to give new columns to DataFrame, with examples.

Syntax

The syntax of pandas DataFrame assign() method is

</>
Copy
DataFrame.assign(**kwargs)

where

ParameterValueDescription
**kwargsdict of {str: callable or Series}The column names are keywords. 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 doesn’t check it). If the values are not callable, (e.g. a Series, scalar, or array), they are simply assigned.

Return Value

DataFrame.

Examples

Assign New column to a DataFrame

In the following program, we take a DataFrame and add a new column named col_2 using the assign function.

Example.py

</>
Copy
import pandas as pd

df = pd.DataFrame({'col_1': [1, 2, 4, 8]})

result = df.assign(col_2=lambda x: x.col_1 * 5)
print(result)

Output

   col_1  col_2
0      1      5
1      2     10
2      4     20
3      8     40

Assign New column to the DataFrame by Reference

In the following program, we take a DataFrame and add a new column named “col_2” using the assign function and by referencing the already existing column in the DataFrame/series.

Example.py

</>
Copy
import pandas as pd

df = pd.DataFrame({'col_1': [1, 2, 4, 8]})

result = df.assign(col_2=df['col_1']*5)
print(result)

Output

   col_1  col_2
0      1      5
1      2     10
2      4     20
3      8     40

Assign New Multiple Columns with assign() Function

In the following program, we create multiple columns within the same assign function.

Example.py

</>
Copy
import pandas as pd

df = pd.DataFrame({'col_1': [1, 2, 4, 8]})

result = df.assign(
    col_2=df['col_1']*5,
    col_3=df['col_1']*100
)

print(result)

Output

   col_1  col_2  col_3
0      1      5    100
1      2     10    200
2      4     20    400
3      8     40    800

Conclusion

In this Pandas Tutorial, we learned how to assign new columns to this DataFrame using pandas DataFrame.assign() method.