• 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 / Append a New Row in a Dataframe in Python

Append a New Row in a Dataframe in Python

Author: Aditya Raj
Last Updated: March 11, 2022

To handle tabular data in python, we normally use dataframes. In this article, we will discuss how we can append a new row in a dataframe.

Append a New Row in a Dataframe Using the loc[] Attribute

If we have a row given in the form of a list, we can use the loc[] property defined in the pandas module to add the row to the dataframe. The loc property is used to get the row at a specific position in a dataframe. When invoked on a dataframe, it takes the row number as the input inside the square brackets and returns a slice of the dataframe. 

To append the list as a new row into the dataframe, we will assign the list to the slice at the last row of the dataframe. 

To get the position of the last row in the dataframe, we can find the length of the dataframe using the len() function. After getting the length of the dataframe, we can add the list as a new row to the dataframe as shown below.

import pandas as pd

df = pd.read_csv('Demo.csv')
print("The dataframe before the append operation:")
print(df)
values = [10, "Sam", "Typescript"]
length = len(df)
df.loc[length] = values
print("The dataframe after the append operation:")
print(df)

Output:

The dataframe before the append operation:
   Roll    Name Language
0     1  Aditya   Python
1     2     Sam     Java
2     3   Chris      C++
The dataframe after the append operation:
   Roll    Name    Language
0     1  Aditya      Python
1     2     Sam        Java
2     3   Chris         C++
3    10     Sam  Typescript

In this approach, we have appended a list as a new row to the data frame. Now, let us discuss an approach to append a python dictionary to the dataframe.

Append a New Row in a Dataframe Using the append() Method

If we are given a dictionary in which the keys of the dictionary consist of the column names of the dataframe, we can add the dictionary as a row into the dataframe using the append() method. The append() method, when invoked on a dataframe, takes a python dictionary as its input argument and appends the values of the dictionary to the dataframe in the last row. Also, we need to assign the value True to the ignore_index parameter of the dataframe. After execution, the append() method returns the updated dataframe. You can observe this in the following example.

import pandas as pd

df = pd.read_csv('Demo.csv')
print("The dataframe before the append operation:")
print(df)
valueDict = {'Roll': 15, 'Name': "Wilson", 'Language': "Golang"}
length = len(df)
df = df.append(valueDict, ignore_index=True)
print("The dataframe after the append operation:")
print(df)

Output:

The dataframe before the append operation:
   Roll    Name Language
0     1  Aditya   Python
1     2     Sam     Java
2     3   Chris      C++
The dataframe after the append operation:
   Roll    Name Language
0     1  Aditya   Python
1     2     Sam     Java
2     3   Chris      C++
3    15  Wilson   Golang

Conclusion

In this article, we have discussed two ways to append a new row in a dataframe in python. To learn more about python programming, you can read this article on list comprehension in python. You might also like this article on dictionary comprehension in python.

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