Assign New Column to Pandas DataFrame

Looking to assign a new column to Pandas DataFrame?

If so, you may use this template to assign your new column:

df.assign(new_column_name = ['item_1', 'item_2', 'item_3', ...])

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

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

Case 1: Assign a Single Column to Pandas DataFrame

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

import pandas as pd

data = {'product_name': ['tablet', 'printer', 'laptop', 'monitor']}
df = pd.DataFrame(data)

print (df)

This is how the DataFrame would look like in Python:

  product_name
0       tablet
1      printer
2       laptop
3      monitor

Now, let’s suppose that you want to assign 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 assign a new column to Pandas DataFrame is:

df.assign(new_column_name = ['item_1', 'item_2', 'item_3', ...])

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

import pandas as pd

data = {'product_name': ['tablet', 'printer', 'laptop', 'monitor']}
df = pd.DataFrame(data)

df = df.assign(price = [250, 100, 1200, 300])
print (df) 

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

  product_name  price
0       tablet    250
1      printer    100
2       laptop   1200
3      monitor    300

Note that the number of items 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: Assign Multiple Columns to Pandas DataFrame

What if you want to assign 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 assign two columns to your DataFrame:

  • The ‘price’ column; and
  • The ‘brand’ column

You can then apply the following code in order to assign those two columns to the DataFrame:

import pandas as pd

data = {'product_name': ['tablet', 'printer', 'laptop', 'monitor']}
df = pd.DataFrame(data)

df = df.assign(price = [250, 100, 1200, 300], brand = ['aa', 'bb', 'cc', 'dd'])
print (df)

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

  product_name  price  brand
0       tablet    250     aa
1      printer    100     bb
2       laptop   1200     cc
3      monitor    300     dd

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