Add Values at the Bottom of pandas DataFrame in Python (Example Code)

On this page you’ll learn how to concatenate values at the bottom of a pandas DataFrame in the Python programming language.

Setting up the Example

import pandas as pd                                     # Load pandas library
my_df = pd.DataFrame({"A":["yes", "yes", "no", "yes"],  # Construct pandas DataFrame
                     "B":range(11, 15),
                     "C":["hey", "hi", "huhu", "hello"]})
print(my_df)
#      A   B      C
# 0  yes  11    hey
# 1  yes  12     hi
# 2   no  13   huhu
# 3  yes  14  hello
my_list = [111, 222, 333]                               # Construct list object
print(my_list)
# [111, 222, 333]

Example: Apply loc() Function to Add Values at the Bottom of DataFrame

my_df_updated = my_df.copy()                            # Append values to DataFrame
my_df_updated.loc[4] = my_list
print(my_df_updated)
#      A    B      C
# 0  yes   11    hey
# 1  yes   12     hi
# 2   no   13   huhu
# 3  yes   14  hello
# 4  111  222    333

Further Resources & Related Articles

Also, you may want to read the related tutorials on this homepage. You can find a selection of articles below:

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top