Skip to content

How to add a new column to an existing DataFrame?

You can add a new column to an existing pandas DataFrame by using the assign() method or the [] notation.

Using the assign() method:

Add a new column to an existing Pandas DataFrame using assign method in Python

python
df = df.assign(new_column_name=new_column_values)

<div class="alert alert-info flex not-prose"> Watch a course Python - The Practical Guide</div>

Using the [] notation:

Add a new column to an existing Pandas DataFrame using [] in Python

python
df['new_column_name'] = new_column_values

Note that new_column_values should be a list or array of the same length as the number of rows in the DataFrame.

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.