Open In App

Append dictionary to data frame

Last Updated : 04 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Appending a dictionary to a DataFrame is a frequent requirement when working with structured data in Python. For example, if we have a DataFrame containing sales data and need to dynamically add details of a new transaction stored in a dictionary, this operation becomes essential. Let's explores multiple ways to append a dictionary to a DataFrame,.

Using loc for Adding Rows

This is one of the fastest and most direct ways to append a dictionary as a new row to a DataFrame. The loc method assigns the dictionary directly to the row at the desired index.

Python
import pandas as pd

# Initial DataFrame
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})


# Dictionary to append
new_row = {'A': 5, 'B': 6}


# Append the dictionary using loc
df.loc[len(df)] = new_row


print(df)

Output
   A  B
0  1  3
1  2  4
2  5  6

Explanation:

  • The len(df) is used to find the next available index in the DataFrame.
  • The loc method efficiently assigns the dictionary values to the specified row.
  • This method is highly efficient and avoids unnecessary overhead, making it ideal for small or large DataFrames.

Let's explores some more ways and see how we can append dictionary to data frame.

Using pd.DataFrame and concat

If we prefer to work with Pandas functions, we can convert the dictionary into a single-row DataFrame and concatenate it with the original DataFrame.

Python
import pandas as pd

# Initial DataFrame
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})

# Dictionary to append
new_row = {'A': 5, 'B': 6}

# Convert dictionary to DataFrame and concatenate
df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)

print(df)

Output
   A  B
0  1  3
1  2  4
2  5  6

Explanation:

  • Wrapping the dictionary in a list converts it into a DataFrame with a single row.
  • The concat() function combines the two DataFrames while ensuring the index is reset.

Using append (Deprecated in Pandas >= 2.0)

The append method was a straightforward way to add a dictionary as a new row. However, it is deprecated in newer versions of Pandas. It is still useful for older codebases.

Python
import pandas as pd

# Initial DataFrame
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})

# Dictionary to append
new_row = {'A': 5, 'B': 6}

# Convert the dictionary to a single-row DataFrame and concatenate
df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)

print(df)

Output
   A  B
0  1  3
1  2  4
2  5  6

Explanation:

  • This method directly appends the dictionary as a new row.
  • The ignore_index=True ensures that the index is reset after appending.
  • Since it is deprecated, avoid using this method for new code.

Using For Loop

This method manually appends dictionary values by iterating through the DataFrame columns. While it is less efficient, it can be helpful for custom workflows.

Python
import pandas as pd

# Initial DataFrame
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})

# Dictionary to append
new_row = {'A': 5, 'B': 6}

# Append manually using a loop
df.loc[len(df)] = [new_row[col] for col in df.columns]

print(df)

Output
   A  B
0  1  3
1  2  4
2  5  6

Explanation:

  • The for loop ensures only the relevant columns in the dictionary are added to the DataFrame.
  • This method is slower than other approaches due to the manual operation.
  • It is more suitable for scenarios where flexibility is required in data manipulation.

Next Article
Article Tags :
Practice Tags :

Similar Reads