Add Column to pandas DataFrame in Python (2 Examples)

 

In this tutorial, I’ll show how to append a new column to a pandas DataFrame in the Python programming language.

The tutorial consists of two examples for the addition of a new column to a pandas DataFrame. To be more precise, the tutorial will contain these contents:

Let’s start right away!

 

Example Data & Add-On Libraries

We first need to load the pandas library to Python, to be able to use the functions that are contained in the library.

import pandas as pd                                               # Load pandas

The following pandas DataFrame is used as basement for this Python tutorial:

data = pd.DataFrame({"x1":range(15, 20),                         # Create pandas DataFrame
                     "x2":["a", "b", "c", "d", "e"],
                     "x3":range(5, 0, - 1)})
print(data)                                                      # Print pandas DataFrame

 

table 1 DataFrame add column pandas dataframe python

 

Have a look at the table that got returned after running the previous syntax. It shows that our example data has five rows and three columns called “x1”, “x2”, and “x3”.

Next, we have to create a list on Python that we can add as new column to our DataFrame:

new_col = ["new", "so_new", "very_new", "the_newest", "neeeew"]  # Create list
print(new_col)                                                   # Print list
# ['new', 'so_new', 'very_new', 'the_newest', 'neeeew']

Our example list contains several different character strings. Note that the length of this list is equal to the number of rows of our DataFrame.

 

Example 1: Append New Variable to pandas DataFrame Using assign() Function

Example 1 illustrates how to join a new column to a pandas DataFrame using the assign function in Python.

Have a look at the Python syntax below:

data_new1 = data.assign(new_col = new_col)                       # Add new column
print(data_new1)                                                 # Print new DataFrame

 

table 2 DataFrame add column pandas dataframe python

 

After running the previous Python code, the pandas DataFrame with one additional variable shown in Table 2 has been constructed.

 

Example 2: Append New Variable to pandas DataFrame Using Square Brackets

The following Python programming syntax demonstrates how to use square brackets to concatenate a new variable to a pandas DataFrame vertically:

data_new2 = data.copy()                                          # Create copy of original DataFrame
data_new2["new_col"] = new_col                                   # Add new column
print(data_new2)                                                 # Print new DataFrame

 

table 3 DataFrame add column pandas dataframe python

 

After running the previous syntax, exactly the same DataFrame as in Example 1 has been created. However, this time we have used square brackets instead of the assign function.

 

Video, Further Resources & Summary

In case you need further info on the examples of this tutorial, I recommend having a look at the following video on my YouTube channel. In the video instruction, I’m explaining the Python programming syntax of this article in more detail.

 

 

Do you want to learn even more about the addition of new columns to a pandas DataFrame? Then I recommend having a look at the following video on Corey Schafer’s YouTube channel.

In the video, he illustrates how to add and remove rows and columns to a pandas DataFrame in a live session:

 

 

Furthermore, you might want to have a look at some of the other posts on my website.

 

Summary: This post has illustrated how to merge, combine, and union a new variable to a pandas DataFrame in the Python programming language. If you have additional questions on how to cbind data in Python, tell me about it in the comments section.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top