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

 

table 1 DataFrame append values pandas dataframe python

 

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

 

table 2 DataFrame append values pandas dataframe python

 

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.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

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:

 

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.

 

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.

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

Menu
Top