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
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 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).
Examples
Let’s see some of the different use-cases of the append()
function through some examples –
1. Append rows of another dataframe
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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
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
2. Append rows with a mismatch in columns
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)
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 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 NaN
s for rows that do not have its value.
Note that you can also use the pandas concat() function to concatenate dataframes.
3. Append a list as a row to a dataframe
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
More on Pandas DataFrames –
- Pandas – Sort a DataFrame
- Change Order of Columns of a Pandas DataFrame
- Pandas DataFrame to a List in Python
- Pandas – Count of Unique Values in Each Column
- Pandas – Replace Values in a DataFrame
- Pandas – Filter DataFrame for multiple conditions
- Pandas – Random Sample of Rows
- Pandas – Random Sample of Columns
- Save Pandas DataFrame to a CSV file
- Pandas – Save DataFrame to an Excel file
- Create a Pandas DataFrame from Dictionary
- Convert Pandas DataFrame to a Dictionary
- Drop Duplicates from a Pandas DataFrame
- Concat DataFrames in Pandas
- Append Rows to a Pandas DataFrame
- Compare Two DataFrames for Equality in Pandas
- Get Column Names as List in Pandas DataFrame
- Select One or More Columns in Pandas
- Pandas – Rename Column Names
- Pandas – Drop one or more Columns from a Dataframe
- Pandas – Iterate over Rows of a Dataframe
- How to Reset Index of a Pandas DataFrame?
- Read CSV files using Pandas – With Examples
- Apply a Function to a Pandas DataFrame
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.