How to add one row in existing Pandas DataFrame? (original) (raw)
Last Updated : 03 Apr, 2025
Adding rows to a Pandas DataFrame is a common task in data manipulation and can be achieved using methods like loc[], and concat().
Method 1. Using loc[] – By Specifying its Index and Values
The loc[] method is ideal for directly modifying an existing DataFrame, making it more memory-efficient compared to append() which is now-deprecated. This approach is particularly useful when you know the index where the row should be inserted.
Example : Adding One Row in an existing Pandas DataFrame using loc
Python `
import pandas as pd data = {"Name": ["Alice", "Bob"], "Age": [25, 30]} df = pd.DataFrame(data)
Add a new row using loc[]
df.loc[len(df)] = ["Charlie", 35] print(df)
`
Output
Name Age
0 Alice 25 1 Bob 30 2 Charlie 35
Method 2. Adding Row Using concat()
The concat() function merges two DataFrames along rows (or columns). To add a single row, create it as a DataFrame and concatenate it with the original. Ideal for adding multiple rows or when working with external data sources. It avoids modifying the original DataFrame directly.
Python `
import pandas as pd data = {"Name": ["Alice", "Bob"], "Age": [25, 30]} df = pd.DataFrame(data)
new_row = pd.DataFrame({"Name": ["Eve"], "Age": [28]}) df = pd.concat([df, new_row], ignore_index=True) print(df)
`
Output
Name Age
0 Alice 25 1 Bob 30 2 Eve 28
Additional Examples of Adding Rows to Pandas DataFrames
Example 1: Adding a Row with Default Values
When you need to add a placeholder row with default values for further updates or processing.
Python `
import pandas as pd data = {"Name": ["Alice", "Bob"], "Age": [25, 30]} df = pd.DataFrame(data)
Add a placeholder row
df.loc[len(df)] = ["Unknown", 0] print(df)
`
Output
Name Age
0 Alice 25 1 Bob 30 2 Unknown 0
Example 2: Adding Multiple Rows with concat()
Useful when merging datasets from different sources, such as combining population data for different cities, ensuring scalability for multiple rows and avoiding direct modification of the original DataFrame.
Python `
import pandas as pd data = {"Name": ["Alice", "Bob"], "Age": [25, 30]} df = pd.DataFrame(data)
New rows as a DataFrame
new_rows = pd.DataFrame({"Name": ["Charlie", "Diana"], "Age": [35, 28]}) df = pd.concat([df, new_rows], ignore_index=True) print(df)
`
Output
Name Age
0 Alice 25 1 Bob 30 2 Charlie 35 3 Diana 28
Example 3: Adding a Row with a Dictionary
Adding a row dynamically with values generated programmatically.
Python `
import pandas as pd data = {"Product": ["A", "B"], "Price": [100, 150]} df = pd.DataFrame(data)
Add a dynamically generated row
new_row = {"Product": "C", "Price": 200} df.loc[len(df)] = new_row print(df)
`
Output
Product Price 0 A 100 1 B 150 2 C 200
Example 4: Adding Rows Conditionally
Adding rows only if certain conditions are met.
Python `
import pandas as pd data = {"Employee": ["Alice", "Bob"], "Salary": [5000, 6000]} df = pd.DataFrame(data)
Add a row only if the salary is below a threshold
if df["Salary"].max() < 7000: df.loc[len(df)] = ["Charlie", 7000] print(df)
`
Output
Employee Salary 0 Alice 5000 1 Bob 6000 2 Charlie 7000
Example 5: Adding Rows Using iloc and Index Reassignment
Adding rows at a specific position in the DataFrame.
Python `
import pandas as pd data = {"City": ["NY", "LA"], "Population": [8_399_000, 3_990_000]} df = pd.DataFrame(data)
Insert a row in the middle
df.loc[1.5] = ["Chicago", 2_705_000] # Add at a floating-point index df = df.sort_index().reset_index(drop=True) print(df)
`
Output
City Population
0 NY 8399000 1 LA 3990000 2 Chicago 2705000
Example 6: Adding Rows with Missing Columns
Handling scenarios where the new row has fewer columns than the original DataFrame.
Python `
import pandas as pd data = {"Name": ["Alice", "Bob"], "Age": [25, 30], "City": ["NY", "LA"]} df = pd.DataFrame(data)
Add a row missing one column
new_row = {"Name": "Charlie", "Age": 35} # No "City" df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True) print(df)
`
Output
Name Age City
0 Alice 25 NY 1 Bob 30 LA 2 Charlie 35 NaN