Append Values to pandas DataFrame in Python (Example)
In this post you’ll learn how to add values at the bottom of a pandas DataFrame in Python programming.
The article looks as follows:
Let’s dive into it.
Example Data & Software Libraries
First, we need to load the pandas library:
import pandas as pd # Load pandas library
We’ll use the following data as basement for this Python programming tutorial:
my_data = pd.DataFrame({"x1":["a", "b", "c", "a", "a", "c", "b"], # Create pandas DataFrame "x2":range(13, 20), "x4":range(1, 8), "x3":["a", "b", "c", "d", "e", "f", "g"]}) print(my_data) # Print pandas DataFrame
As you can see based on Table 1, our example data is a DataFrame and contains seven rows and four columns.
For the example of this tutorial, we also need to create a list of values that we can append to our DataFrame:
my_values = ["w", "x", "y", "z"] # Create list of values print(my_values) # Print list of values # ['w', 'x', 'y', 'z']
Our example list contains the four character values “w”, “x”, “y”, and “z”.
Note that it is important that the length of this list is equal to the number of columns of our DataFrame (i.e. 4).
Example: Append Values to pandas DataFrame Using loc Attribute
This example demonstrates how to assign the values in our list as a new row to the bottom of our pandas DataFrame.
For this task, we can use the loc attribute as shown below:
data_new = my_data.copy() # Create copy of DataFrame data_new.loc[7] = my_values # Append values as new row print(data_new) # Print updated DataFrame
In Table 2 it is shown that we have created a new pandas DataFrame that is a combination or a union of our example DataFrame and our example list.
The values of our list have been merged vertically at the bottom of our DataFrame.
Video, Further Resources & Summary
In case you need further info on the examples of this post, I recommend watching the following video on my YouTube channel. In the video, I’m explaining the Python programming syntax of this tutorial 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.
Also, you may want to read the related tutorials on this homepage. You can find a selection of articles below:
- Append Multiple pandas DataFrames in Python
- Append pandas DataFrame in Python
- Insert Row at Specific Position of pandas DataFrame
- Replace Blank Values by NaN in pandas DataFrame in Python
- Drop Rows with Blank Values from pandas DataFrame in Python
- Count Unique Values in Column of pandas DataFrame in Python
- Drop Infinite Values from pandas DataFrame in Python
- Get Values of First Row in pandas DataFrame in Python
- Count Unique Values by Group in Column of pandas DataFrame in Python
- Change pandas DataFrames in Python
- DataFrame Manipulation Using pandas in Python
- Introduction to the pandas Library in Python
- The Python Programming Language
In summary: This page has demonstrated how to concatenate and join values at the bottom of a pandas DataFrame in Python programming. If you have further questions, let me know in the comments below.