Table of Contents
ToggleIn today’s data-driven world, Pandas DataFrame manipulation is a must-have skill. Whether you need to insert rows, update values, or delete rows by index/condition — mastering these operations will save you time and make your data analysis workflow seamless.
drop()
In this guide, you’ll learn practical examples using df.loc[]
, df.drop()
,
concat()
, assign()
, and even how to perform Pandas Upsert
(insert or update). This article is optimized around popular queries like
“insert row pandas”, “pandas update row”, and
“delete row pandas by condition”.
Pandas doesn’t have a df.insert_row()
API (note: DataFrame.insert()
is for columns), but there are reliable ways to
add single or multiple rows. Use loc
for one-off rows and pd.concat()
for batches.
loc[]
to add a single row (by index)Set a new index label (typically the next integer) and assign a list/dict with values in column order.
import pandas as pd
# Original DataFrame
df = pd.DataFrame({
"Name": ["Amit", "Sneha"],
"Age": [25, 30]
})
# Insert a new row at index 2 (end)
df.loc[2] = ["Ravi", 28]
print(df)
Name Age
0 Amit 25
1 Sneha 30
2 Ravi 28
pd.concat()
for multiple rows (recommended)Build a DataFrame with new records and concatenate once. This is the efficient replacement for deprecated df.append()
.
# New rows to add
new_rows = pd.DataFrame({
"Name": ["Karan", "Meera"],
"Age": [27, 24]
})
# Concatenate and reindex
df = pd.concat([df, new_rows], ignore_index=True)
print(df)
To insert between rows, slice around the target position and concatenate.
# Insert a single row at position i (before row i)
i = 1
new_row_df = pd.DataFrame([{"Name": "Isha", "Age": 26}])
df = pd.concat([df.iloc[:i], new_row_df, df.iloc[i:]], ignore_index=True)
print(df)
pd.concat()
.
df.append()
or concat()
inside tight loops—costly for large data.
NaN
. Use df.astype()
if dtypes matter.
Need to fix or modify data? Pandas makes it easy to update rows and columns by index, by condition, or in bulk. Below are the most reliable patterns you’ll use in real projects.
Target the cell with loc[row_index, column]
or assign a whole row with a list/dict.
import pandas as pd
df = pd.DataFrame({
"Name": ["Amit", "Sneha"],
"Age": [25, 30]
})
# Update a single cell (index 1, column 'Age')
df.loc[1, "Age"] = 31
# Replace the entire row at index 0
df.loc[0] = {"Name": "Ravi", "Age": 29}
print(df)
Use a boolean condition to selectively update values.
# Increase Age by 1 where Name == 'Amit'
df.loc[df["Name"] == "Amit", "Age"] += 1
# Set Age = 0 where Age is missing (example condition)
df.loc[df["Age"].isna(), "Age"] = 0
Assign a dict to multiple columns for the matched rows.
# Update multiple columns where Name == 'Sneha'
df.loc[df["Name"] == "Sneha", ["Name", "Age"]] = ["Sneha", 32]
mask
/where
for functional stylemask
replaces values where condition is True; where
keeps values where condition is True.
# Add 5 years for all ages >= 30
df["Age"] = df["Age"].mask(df["Age"] >= 30, df["Age"] + 5)
# Keep ages <= 40, set others to 40
df["Age"] = df["Age"].where(df["Age"] <= 40, 40)
DataFrame.update()
)Apply values from another DataFrame to matching indexes/columns (non-NA from the right-hand side overwrite). Ensure indexes/columns align first.
# Base data
df_base = pd.DataFrame({"Name": ["Amit", "Sneha"], "Age": [26, 30]}).set_index("Name")
# Incoming updates (Sneha becomes 33)
df_updates = pd.DataFrame({"Name": ["Sneha"], "Age": [33]}).set_index("Name")
# Apply non-null updates where index & columns match
df_base.update(df_updates)
print(df_base.reset_index())
assign()
for readable pipelines:
df = df.assign(Age=df["Age"] + 1)
df[df["Age"]>30]["Age"]=0
).
Use loc
to prevent SettingWithCopyWarning.
reset_index()
, verify index labels before updating.
Cleaning datasets often requires removing rows—by index, by condition, or by position. Pandas offers multiple safe and efficient ways to delete rows.
import pandas as pd
df = pd.DataFrame({
"Name": ["Amit", "Sneha", "Ravi"],
"Age": [25, 30, 28]
})
# Drop row with index 1
df = df.drop(index=1)
print(df)
Name Age
0 Amit 25
2 Ravi 28
# Drop rows where Age > 27
df = df[df["Age"] <= 27]
# OR use query syntax
df = df.query("Age <= 27")
This returns a filtered DataFrame; reindex if needed with reset_index(drop=True)
.
# Delete first row
df = df.iloc[1:]
# Delete last row
df = df.iloc[:-1]
# Drop rows at index 0 and 2
df = df.drop(index=[0, 2])
# Drop rows with any missing values
df = df.dropna()
# Drop duplicate rows
df = df.drop_duplicates()
inplace=False
(default) to return a new DataFrame and avoid side effects.
reset_index(drop=True)
.
drop()
calls.
Quick answers to the most searched tasks: insert rows, update by index/condition, and delete rows by index/position/value.
loc
for a single row, or build a small DataFrame and pd.concat
for multiple rows.
# Single row
df.loc[len(df)] = ["Ravi", 28]
# Multiple rows
new_df = pd.DataFrame([{"Name":"Karan","Age":27},{"Name":"Meera","Age":24}])
df = pd.concat([df, new_df], ignore_index=True)
loc[row_index, column]
or replace an entire row with a dict.
# Update cell
df.loc[1, "Age"] = 31
# Replace entire row
df.loc[0] = {"Name":"Ravi","Age":29}
loc
, or mask
/where
for a functional style.
# Add 1 year where Name == "Amit"
df.loc[df["Name"]=="Amit", "Age"] += 1
# Cap ages at 40
df["Age"] = df["Age"].where(df["Age"] <= 40, 40)
drop(index=...)
or filter with iloc
for first/last rows.
# By index label
df = df.drop(index=1)
# First/last row
df = df.iloc[1:] # drop first
df = df.iloc[:-1] # drop last
query
.
# Keep Age <= 27
df = df[df["Age"] <= 27]
# Equivalent with query
df = df.query("Age <= 27")
DataFrame.update()
for in-place overwrite of non-NA values.
# Example: upsert by 'Name' as key
base = df.set_index("Name")
incoming = pd.DataFrame({"Name":["Sneha","Rohit"], "Age":[33,22]}).set_index("Name")
# Update existing keys; doesn't add new indexes
base.update(incoming)
# Add new keys via concat, then groupby last occurrence
combined = pd.concat([base, incoming], axis=0)
df = combined[~combined.index.duplicated(keep="last")].reset_index()
loc
on the original DataFrame, not a view, or call .copy()
when intentionally working on a slice.
# Bad (may warn)
df[df["Age"] > 30]["Age"] = 0
# Good
df.loc[df["Age"] > 30, "Age"] = 0
DataFrame.append()
is deprecated. Build a DataFrame of new rows and use pd.concat([...], ignore_index=True)
.
Test your knowledge of DataFrame operations. Select the best answer for each. Results and explanations appear at the end.
Score: 0/5
Great job! 🎉 You’ve covered the essential operations to insert rows, update values, and delete rows in Pandas—core skills for fast, reliable data cleaning and transformation.
loc
(single) and pd.concat()
(bulk or at a position via slicing).loc
(by index/condition), mask
/where
, or DataFrame.update()
(merge changes).drop(index=...)
, boolean filters (fast), or position with iloc
; reindex using reset_index(drop=True)
.Level up your analytics career: Join our Data Analytics Course in Dehradun.
📞 Call/WhatsApp: 9411778145