Add Python Dictionary to Pandas DataFrame as Row

This tutorial will provide you with a step-by-step guide on adding a Python dictionary to a Pandas  DataFrame.

By the end of this tutorial, you’ll be able to inject dictionary data into DataFrames using different methods such as loc, iloc.

 

 

Using loc[]

You can add a dictionary as a new row in a Pandas DataFrame is by using the loc[] property.

This approach gives you the advantage of explicitly specifying the index label for the new row.

Here’s the code to add a new row to a DataFrame using loc[].

import pandas as pd
df = pd.DataFrame({
    'UserID': [101, 102, 103],
    'DataUsage': [20.5, 15.3, 32.1],
    'PlanType': ['Basic', 'Premium', 'Basic']
}, index=['a', 'b', 'c'])
new_row = {'UserID': 105, 'DataUsage': 18.2, 'PlanType': 'Basic'}
df.loc['d'] = new_row
print(df)

Output:

   UserID  DataUsage PlanType
a     101       20.5    Basic
b     102       15.3  Premium
c     103       32.1    Basic
d     105       18.2    Basic

In this output, a new row is added to the DataFrame with the index label ‘d’.

 

Using iloc[]

The iloc[] property is another method to add a dictionary as a new row in a Pandas DataFrame.

This method allows you to insert a new row at a specific numerical index.

Add Dictionary at the End

To add a new row at the end of the DataFrame, you can use the following code.

import pandas as pd
df = pd.DataFrame({
    'UserID': [101, 102, 103],
    'DataUsage': [20.5, 15.3, 32.1],
    'PlanType': ['Basic', 'Premium', 'Basic']
})
new_row = {'UserID': 106, 'DataUsage': 24.0, 'PlanType': 'Premium'}

# Add a new row using iloc[]
df.iloc[-1] = new_row
print(df)

Output:

   UserID  DataUsage PlanType
0     101       20.5    Basic
1     102       15.3  Premium
2     106       24.0  Premium

In this output, the last row of the DataFrame is replaced with the new row.

Add Dictionary at a Specific Index

To insert the dictionary at a specific index, you can use the following technique.

# Shift down all rows from index 1
df.iloc[2:] = df.iloc[1:-1].values

# Add new row at index 1
new_row = {'UserID': 107, 'DataUsage': 19.8, 'PlanType': 'Basic'}
df.iloc[1] = new_row
print(df)

Output:

   UserID  DataUsage PlanType
0     101       20.5    Basic
1     107       19.8    Basic
2     102       15.3  Premium

Here, the original row at index 1 is shifted down to make room for the new row.

 

Convert Dictionary to a Pandas Series

Converting the dictionary to a Series allows you to benefit from additional functionalities provided by Series objects, like specifying the name attribute which can serve as the index label when appended to the DataFrame.

Conversion and Addition

Here’s the code snippet to convert a dictionary to a Pandas Series and then add it to an existing DataFrame.

import pandas as pd
df = pd.DataFrame({
    'UserID': [101, 102, 103],
    'DataUsage': [20.5, 15.3, 32.1],
    'PlanType': ['Basic', 'Premium', 'Basic']
})
new_row_dict = {'UserID': 108, 'DataUsage': 17.6, 'PlanType': 'Premium'}
new_row_series = pd.Series(new_row_dict)
df.loc['e'] = new_row_series
print(df)

Output:

   UserID  DataUsage PlanType
0     101       20.5    Basic
1     102       15.3  Premium
2     103       32.1    Basic
e     108       17.6  Premium

In this output, the new row is successfully added to the DataFrame.

 

Use DataFrame.reindex

The reindex method is another way to add a new row to a Pandas DataFrame using dictionary values.

Insert at a Specific Index

To insert a new row at a specific index using reindex, you can perform the following steps:

import pandas as pd
df = pd.DataFrame({
    'UserID': [101, 102, 103],
    'DataUsage': [20.5, 15.3, 32.1],
    'PlanType': ['Basic', 'Premium', 'Basic']
}, index=[0, 1, 2])

new_row_dict = {'UserID': 109, 'DataUsage': 23.4, 'PlanType': 'Basic'}

# Reindex the DataFrame
new_index = [0, 1, 2, 3]  # Adding a new index
df = df.reindex(new_index)

# Insert new row at index 3
df.loc[3] = new_row_dict
print(df)

Output:

   UserID  DataUsage PlanType
0   101.0       20.5    Basic
1   102.0       15.3  Premium
2   103.0       32.1    Basic
3   109.0       23.4    Basic

In this output, the DataFrame is reindexed to include a new index ‘3’. After reindexing, the new row is added at this specific index.

Leave a Reply

Your email address will not be published. Required fields are marked *