Python Pandas Example | Append Rows to a DataFrame

This python pandas example will show us how to append some rows to a dataframe.

import pandas as pd

# create a sample dataframe
data1 = {
    'Name': ['Microsoft Corporation', 'Google, LLC', 'Tesla, Inc.'],
    'Symbol': ['MSFT', 'GOOG', 'TSLA'],
    'Shares': [100, 50, 150]
}
df1 = pd.DataFrame(data1)
# print the original dataframe
print("The original dataframe:\n")
print(df1)

# The dataframe to append
data2 = {
    'Name':['Apple Inc.', 'Netflix, Inc.'],
    'Symbol':['APPL', 'NFLX'],
    'Shares': [200, 80]
}
df2 = pd.DataFrame(data2)
# print the dataframe to append
print("\nThe dataframe to append:\n")
print(df2)

# Append rows
df3 = df1.append(df2)
print("\nThe appended dataframe:\n")
print(df3)

Run this code, we will see:

The original dataframe:

                    Name Symbol  Shares
0  Microsoft Corporation   MSFT     100
1            Google, LLC   GOOG      50
2            Tesla, Inc.   TSLA     150

The dataframe to append:

            Name Symbol  Shares
0     Apple Inc.   APPL     200
1  Netflix, Inc.   NFLX      80

The appended dataframe:

                    Name Symbol  Shares
0  Microsoft Corporation   MSFT     100
1            Google, LLC   GOOG      50
2            Tesla, Inc.   TSLA     150
0             Apple Inc.   APPL     200
1          Netflix, Inc.   NFLX      80