• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Pandas Append Row to DataFrame

Pandas Append Row to DataFrame

Author: Aditya Raj
Last Updated: March 22, 2023

The pandas module provides different methods to add and remove rows from a dataframe. In this article, we will discuss different ways to append a row to pandas dataframe using the append() method and the concat() function. 

Table of Contents
  1. The Pandas append() Method
  2. Append Row at the Top of a Pandas DataFrame
    1. Pandas Append Row at the Top of a DataFrame Using The concat() Function
  3. Append a Row at The Bottom of a DataFrame
    1. Pandas Append Row at the Bottom of a DataFrame Using The concat() Function
  4. Conclusion

The Pandas append() Method

We use the append() method to append a dictionary, series, or dataframe object to another dataframe. It has the following syntax.

DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=False)

Here, 

  • The other parameter takes a pandas Series, dictionary, or another dataframe as its input argument.
  • We use the ignore_index parameter to specify if we want to preserve the index of the original dataframes. By default, it is set to False, which means that the output dataframe contains indices from the original dataframes. To reset the indices and create a new index in the output dataframe, you can set the ignore_index parameter to True.
  • We use the verify_integrity parameter to specify if we want to allow duplicate indices in the output dataframe or not. By default, the verify_integrity parameter is set to False. It means that the output dataframe can contain duplicate indices. To disallow duplicate indices, you can set the verify_integrity parameter to True. 
  • We use the sort parameter to specify if we want to sort columns if the columns of the dataframes are not aligned.

After execution, the append() method returns a new dataframe. 

Append Row at the Top of a Pandas DataFrame

To append a row at the top of a dataframe, we will use the append() method and the DataFrame() function.

Suppose that we want to append a new python dictionary as a row to an existing dataframe. For this, we will use the following steps.

  • First, we will put the dictionary containing the row values into a list.
  • Next, we will create a dataframe using the list and the DataFrame() function. The DataFrame() function takes the list containing the dictionary as its input and returns a dataframe after execution. 
  • Now, we will invoke the append() method on the newly created dataframe and pass the existing dataframe as its input argument. 

After execution of the append() method, we will get the output dataframe with a new row appended at the top. You can observe this in the following example.

import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
        {"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
        {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
        {"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
        {"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
        {"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The original dataframe is:")
print(df)
newDict= {"Roll":11,"Maths":81, "Physics":74, "Chemistry": 93}
print("New row data is:")
print(newDict)
df1=pd.DataFrame([newDict])
df2=df1.append(df,ignore_index=True)
print("The output dataframe is:")
print(df2)

Output:

The original dataframe is:
   Roll  Maths  Physics  Chemistry
0     1    100       80         90
1     2     80      100         90
2     3     90       80         70
3     4    100      100         90
4     5     90       90         80
5     6     80       70         70
New row data is:
{'Roll': 11, 'Maths': 81, 'Physics': 74, 'Chemistry': 93}
The output dataframe is:
   Roll  Maths  Physics  Chemistry
0    11     81       74         93
1     1    100       80         90
2     2     80      100         90
3     3     90       80         70
4     4    100      100         90
5     5     90       90         80
6     6     80       70         70

The append() method will be deprecated from the upcoming pandas versions. Therefore, you can use the concat() function to concatenate the dataframes.

Pandas Append Row at the Top of a DataFrame Using The concat() Function

The contact() function takes a list of dataframes as its input argument and concatenates them into a single dataframe. As we want to append a new row to an existing dataframe, we will pass the dataframe containing the new row as the first element and the existing dataframe as the second element of the input list to the concat() function.

After execution of the concat() function, we will get the desired output dataframe as shown below.

import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
        {"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
        {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
        {"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
        {"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
        {"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The original dataframe is:")
print(df)
newDict= {"Roll":11,"Maths":81, "Physics":74, "Chemistry": 93}
print("New row data is:")
print(newDict)
df1=pd.DataFrame([newDict])
df2=pd.concat([df1,df],ignore_index=True)
print("The output dataframe is:")
print(df2)

Output:

The original dataframe is:
   Roll  Maths  Physics  Chemistry
0     1    100       80         90
1     2     80      100         90
2     3     90       80         70
3     4    100      100         90
4     5     90       90         80
5     6     80       70         70
New row data is:
{'Roll': 11, 'Maths': 81, 'Physics': 74, 'Chemistry': 93}
The output dataframe is:
   Roll  Maths  Physics  Chemistry
0    11     81       74         93
1     1    100       80         90
2     2     80      100         90
3     3     90       80         70
4     4    100      100         90
5     5     90       90         80
6     6     80       70         70

In the above examples, we have appended a new row at the top of a dataframe using the append() method and the concat() method one by one.

Append a Row at The Bottom of a DataFrame

To append a row at the bottom of a dataframe, we just need to invoke the append() method on the original dataframe and pass the python dictionary containing the row data as an input argument. After execution of the append() method, we will get the desired output dataframe as shown below.

import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
        {"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
        {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
        {"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
        {"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
        {"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The original dataframe is:")
print(df)
newDict= {"Roll":11,"Maths":81, "Physics":74, "Chemistry": 93}
print("New row data is:")
print(newDict)
df2=df.append(newDict,ignore_index=True)
print("The output dataframe is:")
print(df2)

Output:

The original dataframe is:
   Roll  Maths  Physics  Chemistry
0     1    100       80         90
1     2     80      100         90
2     3     90       80         70
3     4    100      100         90
4     5     90       90         80
5     6     80       70         70
New row data is:
{'Roll': 11, 'Maths': 81, 'Physics': 74, 'Chemistry': 93}
The output dataframe is:
   Roll  Maths  Physics  Chemistry
0     1    100       80         90
1     2     80      100         90
2     3     90       80         70
3     4    100      100         90
4     5     90       90         80
5     6     80       70         70
6    11     81       74         93

While using the append() method in the above example, we have set the ignore_index parameter to True. This is necessary because the append() method will run into an error if we don’t do so. While appending a dictionary to the pandas dataframe using the append() method, you always need to set the ignore_index parameter to True.

Pandas Append Row at the Bottom of a DataFrame Using The concat() Function

To append a row at the bottom of a dataframe using the contact() function, you first need to create a dataframe from the dictionary containing the row data.

Then, you can pass the existing dataframe as the first element and the dataframe containing the new row as the second element of the input list to the concat() function. After execution of the concat() function, you will get the desired dataframe as shown in the following example.

import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
        {"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
        {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
        {"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
        {"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
        {"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The original dataframe is:")
print(df)
newDict= {"Roll":11,"Maths":81, "Physics":74, "Chemistry": 93}
print("New row data is:")
print(newDict)
df1=pd.DataFrame([newDict])
df2=pd.concat([df,df1],ignore_index=True)
print("The output dataframe is:")
print(df2)

Output:

The original dataframe is:
   Roll  Maths  Physics  Chemistry
0     1    100       80         90
1     2     80      100         90
2     3     90       80         70
3     4    100      100         90
4     5     90       90         80
5     6     80       70         70
New row data is:
{'Roll': 11, 'Maths': 81, 'Physics': 74, 'Chemistry': 93}
The output dataframe is:
   Roll  Maths  Physics  Chemistry
0     1    100       80         90
1     2     80      100         90
2     3     90       80         70
3     4    100      100         90
4     5     90       90         80
5     6     80       70         70
6    11     81       74         93

As you can observe in the above output, we have appended a new row at the bottom of an existing dataframe using the contact() function.

Conclusion

In this article, we discussed how to append a row to a dataframe. To learn more about python programming, you can read this article on how to convert string to dataframe in python. You might also like this article on how to convert pandas dataframe to a list in python.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary โ€“ How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 ยท PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us