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.
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
df = df.assign(new_column_name=new_column_values)
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Python - The Practical Guide</div>
Using the [] notation:
Add a new column to an existing Pandas DataFrame using [] in Python
df['new_column_name'] = new_column_valuesNote that new_column_values should be a list or array of the same length as the number of rows in the DataFrame.