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.
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