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
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
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
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.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
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:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you might want to have a look at some of the other posts on my website.
- Introduction to the pandas Library in Python
- Insert Column at Specific Position of pandas DataFrame in Python
- Add Row to pandas DataFrame in Python
- Check if Column Exists in pandas DataFrame in Python
- Modify & Edit pandas DataFrames in Python
- pandas DataFrames Operations in Python
- All Python Programming Examples
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.