The 6 AI Engineering Patterns, come build with Greg live:Β Starts Jan 6th, 2025
Leverage
Pandas functions

Pandas Append – pd.DataFrame.append()

Append rows to DataFrame in Pandas

So you want to add more rows to your DataFrame? Sure, just use Pandas Append

Pandas DataFrame.append() will append rows (add rows) of other DataFrame, Series, Dictionary or list of these to another DataFrame.

1. YourDataFrame.append(other=Data_You_Want_To_Append)

I have two main uses for this function:

  • When I want to combine entire DataFrames together
  • When I simply want to add a single row to my DataFrame

Pseudo code: Take your new piece of data, and put it on the end (bottom) of your DataFrame

Pandas Append

Above we are appending DataFrame2 to the end of DataFrame1. To ensure your index comes out clean, make sure to put ignore_index=True.

Pro Tip: If you have many rows to add, it’s computationally cheaper to first combine your data THEN add it to your new DataFrame.

Example: If you have 100s rows to add, instead of .append()-ing 100s times, first combine your 100s rows into a single DataFrame or list of dicts, then .append() once.

This is very similar to python’s regular append.

Append Parameters

  • other: The data that you want to append! This can be:
    • DataFrame: Add one DataFrame to the end of another DataFrame
    • Series: Add a series with index labels of the DataFrame your appending too. ignore_index must be true
    • Dictionary: Simply pass a dictionary who’s keys are the DataFrame columns you’re appending to. ignore_index must be true
  • ignore_index (Default: False): Here you tell pandas if you want to ignore the index of the data your appending. Generally I always select ignore_index=True. If you’re appending a Series or Dict then you’ll need to set this to true
  • verify_integrity (Default: False): This will check if your resulting DataFrame has any duplicate indexes. If it does, pandas will raise an error.
  • sort (Default: False): Sort the new DataFrames columns if the two combined columns do not align (aren’t the same). I generally don’t use this ever.

Here’s a Jupyter notebook showing how to set index in Pandas

Link to code

Official Documentation

On this page