Home βΊ Python & Pandas βΊ Insert, Update & Delete Rows
Table of Contents
ToggleQuick Answer: Use loc for insert and update, and drop for deleting rows in Pandas.
In Pandas, you can insert, update, and delete rows using loc, drop, and assignment operations on a DataFrame.
If you’re working with Python Pandas and need to insert, update, or delete rows, this guide gives you simple, practical examples.
Learn how to update and delete rows with simple Python examples
You can update values in a Pandas DataFrame using loc[], iloc[], or conditions.
import pandas as pd
df = pd.DataFrame({
"Name": ["Amit", "Sneha"],
"Age": [25, 30]
})
df.loc[0, "Age"] = 26
print(df)
df.loc[df["Name"] == "Sneha", "Age"] = 31
df.iloc[1, 1] = 32
You can delete rows using drop() or by applying conditions.
df = df.drop(0)
df = df.drop([0, 1])
df = df[df["Age"] >= 30]
df = df.reset_index(drop=True)
π Learn complete data analytics: Data Analytics Guide
Fix common mistakes while inserting, updating, and deleting rows
This happens when you try to modify a slice of a DataFrame instead of the original data.
# Wrong
df[df["Age"] > 25]["Age"] = 30
β Fix:
df.loc[df["Age"] > 25, "Age"] = 30
This error occurs when the column name is incorrect or misspelled.
# Wrong
df["age"]
β Fix: Check column names:
print(df.columns)
Happens when assigning values that donβt match the DataFrame structure.
# Wrong
df.loc[2] = ["Ravi"]
β Fix: Match all columns:
df.loc[2] = ["Ravi", 28]
After deleting rows, index may look messy.
β Fix:
df = df.reset_index(drop=True)
π Learn Python basics here: Python Practice Questions
Fix common mistakes while inserting, updating, and deleting rows
This happens when you try to modify a slice of a DataFrame instead of the original data.
# Wrong
df[df["Age"] > 25]["Age"] = 30
β Fix:
df.loc[df["Age"] > 25, "Age"] = 30
This error occurs when the column name is incorrect or misspelled.
# Wrong
df["age"]
β Fix: Check column names:
print(df.columns)
Happens when assigning values that donβt match the DataFrame structure.
# Wrong
df.loc[2] = ["Ravi"]
β Fix: Match all columns:
df.loc[2] = ["Ravi", 28]
After deleting rows, index may look messy.
β Fix:
df = df.reset_index(drop=True)
π Learn Python basics here: Python Practice Questions
Apply insert, update, and delete operations in a real dataset
Letβs apply what you learned in a simple real-world project. In this example, we manage customer data using Pandas.
Create a dataset with customer names and ages.
Example: Create a table with Name and Age columns.
Add a new customer record to your dataset.
Use: loc method to insert new row
Modify existing values such as age or name.
Use: loc method for updating values
Remove a customer record if needed.
Use: drop method
After performing all operations, your dataset will be updated with new entries, corrected values, and removed records.
Try this project on real datasets like customer sales or student data to build strong data analytics skills.
π Learn full data science process: Data Science Guide
Practice Pandas operations with real data
To master Pandas insert, update, and delete operations, download the practice dataset and try it yourself.
Practice with customer and product data
Quick revision notes for Pandas operations
Test your knowledge with exercises
Practicing on real datasets is the fastest way to learn data analytics.
π Explore full course: Data Analytics Course
Attempt all questions, then check your answers
1. Which method is used to insert a row in Pandas?
2. Which method is best for updating values?
3. Which function is used to delete rows?
4. What does iloc use?
5. Which method is recommended instead of append?
1 β B (loc)
2 β A (loc)
3 β C (drop)
4 β C (index positions)
5 β B (concat)
Real students. Real transformations. From beginners to professionals.
Vista Certified Data Analyst Program
π Every certificate is a story of transformation and success.
Join Vista Academy & become job-ready for top companies & government roles.
Learn industry tools. Build real dashboards. Crack interviews. Join Vista Academy β Dehradunβs trusted Data & Business Analytics institute.
