The 6 AI Engineering Patterns, come build with Greg live:Β Starts Jan 6th, 2025
Leverage
Pandas functions

Add Column To Dataframe Pandas

Add column to Pandas DataFrame

One of the most common Pandas tasks you’ll do is add more data to your DataFrame. This means you need to become an expert at adding a column to your DataFrame.

5 ways to add a new column to your DataFrame in Pandas:

  • By declaring a new column name with a scalar or list of values
  • By using df.insert()
  • Using df.assign()
  • Using a dictionary
  • Using .loc[]

Pseudo code: Using a new scalar or list of data, add a new column to your DataFrame.

Adding Column To Pandas DataFrame

Let’s take a look at the 5 ways you can add a column to your DataFrame. For examples of these, check out the code below.

Declare new column by referencing a new name

95% of the time you’ll be adding a new column to your dataset by referencing a column name that isn’t already there.

You can add a scalar (a single value) or a list (Series, dict, etc.) of items. Make sure if you add a list it is the same length as your df.

This method will put the new column at the end of your DataFrame (last column).

df['new_column_name'] = 5 # You'll get a column of all 5s
df['new_column_name'] = [1,2,3,4] # Assuming your df is 4 items

Using df.insert()

Insert will put a new column in your DataFrame at a specified location. The main advantage is you get to pick where in your DataFrame you want the column.

df.insert(loc=column_location,
          column='new_column_name',
          value=new_column_values)

Using df.assign()

Assign will also add new columns to your DataFrame, but this time, you can add multiple columns. The entire DataFrame will be returned.

df.assign(new_column=lambda x: x.another_column + 7)

Using A Dictionary

One of the most straight forward ways is to simply use a dictionary. This new dict will add new rows based off of the key values you pass.

people_dict = {'bob': 'boy', 'Mike': 'boy', 
           'Katie': 'girl', 'Stacey': 'girl'}  
df['people'] = people_dict

Using .loc[]

Not recommended, try one of the above methods first.

You could add a new column via the .loc[] methods. This is generally used for data look ups.

df.loc[:,'new_column'] = new_column_series

Here’s a Jupyter notebook with a few examples:

Link to code

Official Documentation

On this page