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:
df = df.assign(new_column_name=new_column_values)
Watch a video course
Python - The Practical Guide
Using the []
notation:
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.