Pandas: How to append a dictionary to a DataFrame (as a new row)

Updated: February 21, 2024 By: Guest Contributor Post a comment

Introduction

Pandas, a powerful and flexible open-source data analysis and manipulation tool built on top of the Python programming language, has become a staple for data scientists and analysts across the globe. One common operation while working with Pandas DataFrames is adding new rows. Specifically, appending a dictionary as a new row to an existing DataFrame can be particularly useful in a wide range of scenarios, from updating your dataset with new information to dynamically collecting and storing data in an organized manner.

This tutorial will guide you through various methods to append a dictionary to a DataFrame in Pandas, starting from basic examples and gradually moving to more advanced techniques. Each section is accompanied by code examples and expected outputs to help you understand the concepts in practice.

Getting Started

Before diving into the examples, ensure you have Pandas installed in your Python environment. You can install Pandas using pip:

pip install pandas

Once installed, you’ll need to import Pandas in your script:

import pandas as pd

Basic Example: Appending a Single Dictionary

Let’s start with the most straightforward scenario where you have a DataFrame and want to append a single dictionary as a new row. Suppose you have the following DataFrame:

import pandas as pd

# Sample DataFrame
data = { 'Name': ['Alice', 'Bob'], 'Age': [25, 30], 'City': ['New York', 'Los Angeles'] }
df = pd.DataFrame(data)
print(df)

Output:

    Name  Age        City
0  Alice   25    New York
1    Bob   30  Los Angeles

To append a dictionary as a new row, use the append() method:

new_row = {'Name': 'Charlie', 'Age': 35, 'City': 'Chicago'}
df = df.append(new_row, ignore_index=True)
print(df)

Output:

      Name  Age         City
0    Alice   25     New York
1      Bob   30   Los Angeles
2  Charlie   35      Chicago

Note that setting ignore_index=True is essential to avoid indexing errors and ensure the new row gets added correctly.

Adding Multiple Dictionaries as Rows

If you have multiple dictionaries that you want to append as rows to your DataFrame, you can leverage the pd.concat() function. Here’s an example:

new_rows = [{'Name': 'Diana', 'Age': 28, 'City': 'Boston'},
            {'Name': 'Evan', 'Age': 22, 'City': 'Seattle'}]
df = pd.concat([df, pd.DataFrame(new_rows)], ignore_index=True)
print(df)

Output:

      Name  Age         City
0    Alice   25     New York
1      Bob   30   Los Angeles
2  Charlie   35      Chicago
3    Diana   28       Boston
4     Evan   22      Seattle

Appending a Dictionary with Missing Columns

What if the dictionary you’re adding as a new row doesn’t have all the columns present in the DataFrame? Pandas manages this scenario elegantly, filling missing columns with NaN values. Observe the following:

new_row = {'Name': 'Fiona', 'Age': 29}
df = df.append(new_row, ignore_index=True)
print(df)

Output:

      Name  Age         City
0    Alice   25     New York
1      Bob   30   Los Angeles
2  Charlie   35      Chicago
3    Diana   28       Boston
4     Evan   22      Seattle
5    Fiona   29          NaN

Appending a Dictionary with Extra Columns

Similarly, if the dictionary contains columns that are not present in the DataFrame, these new columns will be added to the DataFrame, and the existing rows will be filled with NaN for those new columns. For example:

new_row = {'Name': 'George', 'Age': 40, 'City': 'Miami', 'Profession': 'Engineer'}
df = df.append(new_row, ignore_index=True)
print(df)

Output:

      Name  Age         City Profession
0    Alice   25     New York        NaN
1      Bob   30   Los Angeles        NaN
2  Charlie   35      Chicago        NaN
3    Diana   28       Boston        NaN
4     Evan   22      Seattle        NaN
5    Fiona   29          NaN        NaN
6   George   40        Miami   Engineer

Advanced Techniques

For more advanced scenarios, you might want to control the type of the index or handle more complex data structures when appending your dictionary. Here are some tips:

  • Use the pd.Series instead of a dictionary to have control over the index type when adding a single row.
  • When working with nested dictionaries, consider flattening them before appending to better manage the DataFrame structure.

Conclusion

Appending dictionaries to a DataFrame in Pandas is a versatile and essential skill for data manipulation and analysis. Whether dealing with single or multiple rows, handling columns of varying presence across dictionaries, or managing more complex data structures, Pandas provides robust and elegant solutions to incorporate dictionaries into DataFrames efficiently. Through practice and exploration of the examples provided, you’ll become more adept at utilizing these techniques in your data projects.