Add New Column to Pandas DataFrame using Assign

Looking to add a new column to pandas DataFrame?

If so, you can use this simple template to add a new column to your DataFrame using assign:

df.assign(name for the new column to be added = [data for the new column])

To see how to apply this template in practice, I’ll review two cases of:

  • Adding a single column to an existing DataFrame; and
  • Adding multiple columns to a DataFrame

Case 1: Add Single Column to Pandas DataFrame using Assign

To start with a simple example, let’s say that you currently have a DataFrame with a single column about electronic products:

from pandas import DataFrame

Products = {'Product': ['Tablet','iPhone','Laptop','Monitor']}
df = DataFrame(Products, columns= ['Product'])

print (df)

This is how the DataFrame would look like in Python:

Add New Column to Pandas DataFrame using Assign

Now, let’s suppose that you want to add a new column to the DataFrame. The new column will be called ‘Price’ and it will contain the prices associated with the products.

Recall that the template to add a new column to pandas DataFrame is:

df.assign(name for the new column to be added = [data for the new column])

Therefore, the complete code to add the ‘Price’ column to the existing DataFrame is:

from pandas import DataFrame

Products = {'Product': ['Tablet','iPhone','Laptop','Monitor']}
df = DataFrame(Products, columns= ['Product'])

df = df.assign(Price = [250,800,1200,300])
print (df) 

Run the code in Python, and you’ll see the new column:

Assign DataFrame

Note that the number of data points under your new column must match with the length of index in your existing DataFrame. In our example, since we have 4 records (for the 4 products), you must add 4 prices under the new ‘Price’ column. Otherwise, you’ll get the following error:

ValueError: Length of values does not match length of index

Case 2: Add Multiple Columns to Pandas DataFrame

What if you want to add multiple columns to your DataFrame?

If that’s the case, simply separate those columns using a comma.

For example, let’s say that you want to add two columns to your DataFrame:

  • The ‘Price’ column; and
  • The ‘Discount’ column

You can then apply this code in Python to add those two columns to your existing DataFrame:

from pandas import DataFrame

Products = {'Product': ['Tablet','iPhone','Laptop','Monitor']}
df = DataFrame(Products, columns= ['Product'])

df = df.assign(Price = [250,800,1200,300], Discount = ['15%','10%','5%','25%'])
print (df)

Once you run the code in Python, you’ll see those two additional columns:

Add Multiple Column to Pandas DataFrame

You can read more about adding new columns to the DataFrame by visiting the Pandas Documentation.