Add Multiple Columns to pandas DataFrame in Python (Example)
In this tutorial, I’ll illustrate how to append multiple new variables to a pandas DataFrame in the Python programming language.
The content is structured as follows:
So let’s take a look at some Python codes in action:
Example Data & Add-On Libraries
In order to use the functions of the pandas library, we first have to load pandas:
import pandas as pd # Import pandas library
The following data is used as basement for this Python programming tutorial:
data = pd.DataFrame({"x1":range(31, 35), # Create pandas DataFrame "x2":["a", "b", "c", "d"], "x3":range(45, 41, - 1)}) print(data) # Print pandas DataFrame
Table 1 shows that our example data consists of four rows and three columns.
Next, we have to create several list objects that we will add as new columns to our pandas DataFrame later on.
new1 = ["x", "xx", "xxx", "xxxx"] # Create first list print(new1) # Print first list # ['x', 'xx', 'xxx', 'xxxx']
new2 = [1, 2, 3, 4] # Create second list print(new2) # Print second list # [1, 2, 3, 4]
Note that our two lists have the same length as the number of rows of our data set.
Example: Append Multiple Columns to pandas DataFrame
In this example, I’ll demonstrate how to combine multiple new columns with an existing pandas DataFrame in one line of code.
Consider the following python syntax:
data_new = data.copy() # Create copy of DataFrame data_new["new1"], data_new["new2"] = [new1, new2] # Add multiple columns print(data_new) # Print updated pandas DataFrame
By running the previous code, we have created Table 2, i.e. a new pandas DataFrame containing a union of our example data set plus our two list objects.
Video & Further Resources
In case you need further info on how to merge and join new columns to a pandas DataFrame, you could watch the following video on my YouTube channel. I explain the examples of this article in the video instruction.
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.
In addition, you may want to read the related tutorials on my website. You can find a selection of tutorials on related topics such as counting and descriptive statistics below:
- Types of Joins for pandas DataFrames in Python
- Add Column from Another pandas DataFrame
- rbind & cbind pandas DataFrame in Python
- Combine pandas DataFrames Vertically & Horizontally
- Merge List of pandas DataFrames in Python
- Merge pandas DataFrames based on Particular Column
- Merge pandas DataFrames based on Index
- Merge Multiple pandas DataFrames in Python
- Merge Two pandas DataFrames in Python
- Combine pandas DataFrames with Different Column Names
- Combine pandas DataFrames with Same Column Names
- Append Multiple pandas DataFrames in Python
- Append pandas DataFrame in Python
- Count Rows & Columns of pandas DataFrame in Python
- Sum of Columns & Rows of pandas DataFrame in Python
- Change Order of Columns in pandas DataFrame in Python
- Add Column to pandas DataFrame in Python
- pandas DataFrame Operations in Python
- DataFrame Manipulation Using pandas in Python
- Introduction to the pandas Library in Python
- Introduction to Python
In this Python tutorial you have learned how to add and concatenate several new variables to a pandas DataFrame. If you have any additional comments and/or questions, please let me know in the comments. Besides that, don’t forget to subscribe to my email newsletter in order to receive updates on new articles.