Append Rows to a Pandas DataFrame

While working with dataframes, it may happen that you’d want to add a few rows to a dataframe. Pandas dataframes are quite versatile when it comes to handing and manipulating tabular data. Among other features, they allow you the flexibility to append rows to an existing dataframe. In this tutorial, we’ll look at how to append one or more rows to a pandas dataframe through some examples.

The pandas dataframe append() function is used to add one or more rows to the end of a dataframe. The following is the syntax if you say want to append the rows of the dataframe df2 to the dataframe df1

df_new = df1.append(df2)

The append() function returns the a new dataframe with the rows of the dataframe df2 appended to the dataframe df1. Note that the columns in the dataframe df2 not present in df1 are added as new columns (see the examples).

Let’s see some of the different use-cases of the append() function through some examples –

You can append another dataframe’s rows at the end of a dataframe. Pass the dataframe whose rows you want to append as an argument to the append() function.

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)

Output:

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

The returned dataframe df3 has rows of the dataframe df2 appended to the end of the dataframe df1. Note that the resulting dataframe retains the index of the original dataframes. If you want the resulting dataframe to have a fresh continuous index, pass ignore_index=True to the append() function. See the example below:

print(df1.append(df2, ignore_index=True))

Output:

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

Columns that are not present in the original dataframe (the one on which the append function is applied) are added as new columns. See the example below:

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],
    'Market Cap($B)': ['2030', '237']
}
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)

Ouput:

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 Market Cap($B)
0     Apple Inc.   APPL     200           2030
1  Netflix, Inc.   NFLX      80            237

The appended dataframe:

                    Name Symbol  Shares Market Cap($B)
0  Microsoft Corporation   MSFT     100            NaN
1            Google, LLC   GOOG      50            NaN
2            Tesla, Inc.   TSLA     150            NaN
0             Apple Inc.   APPL     200           2030
1          Netflix, Inc.   NFLX      80            237

In the above example, you can see that the dataframe df2 has a column Market Cap($B) which is not present in the dataframe df1. This new column is present in the returned dataframe from the append() function with NaNs for rows that do not have its value.

Note that you can also use the pandas concat() function to concatenate dataframes.

If you want to append a list as a row to a pandas dataframe you can convert it to a pandas series first and then use the append() function to add it to the dataframe. See the example below:

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 list to append as row
ls = ['Apple Inc.', 'APPL', 200]
# Create a pandas series from the list
row = pd.Series(ls, index=df1.columns)
# print the row
print("\nThe row to append:\n")
print(row)

# Append the row to the dataframe
df3 = df1.append(row, ignore_index=True)
print("\nThe appended dataframe:\n")
print(df3)

Output:

The original dataframe:

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

The row to append:

Name      Apple Inc.
Symbol          APPL
Shares           200
dtype: object

The appended dataframe:

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

Note that in the above case of appending a pandas series to a dataframe using the append() function, we provided ignore_index=True. If it’s not passed, it results in an error.

For more on the pandas append() function, refer to its official documentation.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.