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).
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.
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.
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.
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.
Hereβs a Jupyter notebook with a few examples: