Python: A Deep Dive into Data Structures for Efficient Programming

Home β€Ί Python & Pandas β€Ί Insert, Update & Delete Rows

Pandas Insert, Update & Delete Rows (With Examples)

Quick Answer: Use loc for insert and update, and drop for deleting rows in Pandas.

Quick Answer

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.

Insert Rows
Update Values
Delete Rows

✏️ Update & πŸ—‘οΈ Delete Rows in Pandas DataFrame

Learn how to update and delete rows with simple Python examples

πŸ‘‰ How to Update Rows in Pandas

You can update values in a Pandas DataFrame using loc[], iloc[], or conditions.

πŸ“Œ Method 1: Update using loc[]

import pandas as pd

df = pd.DataFrame({
    "Name": ["Amit", "Sneha"],
    "Age": [25, 30]
})

df.loc[0, "Age"] = 26
print(df)

πŸ“Œ Method 2: Update using condition

df.loc[df["Name"] == "Sneha", "Age"] = 31

πŸ“Œ Method 3: Update using iloc[]

df.iloc[1, 1] = 32

πŸ‘‰ How to Delete Rows in Pandas

You can delete rows using drop() or by applying conditions.

πŸ“Œ Method 1: Delete row by index

df = df.drop(0)

πŸ“Œ Method 2: Delete multiple rows

df = df.drop([0, 1])

πŸ“Œ Method 3: Delete rows using condition

df = df[df["Age"] >= 30]

πŸ“Œ Method 4: Reset index

df = df.reset_index(drop=True)

πŸ’‘ Pro Tips

  • Use loc[] for label-based updates
  • Use iloc[] for position-based updates
  • Always reset index after deleting rows
  • Avoid modifying original data without backup

πŸ‘‰ Learn complete data analytics: Data Analytics Guide

⚠️ Common Errors in Pandas (And How to Fix Them)

Fix common mistakes while inserting, updating, and deleting rows

❌ Error 1: SettingWithCopyWarning

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

❌ Error 2: KeyError (Column Not Found)

This error occurs when the column name is incorrect or misspelled.

# Wrong
df["age"]

βœ… Fix: Check column names:

print(df.columns)

❌ Error 3: ValueError (Length Mismatch)

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]

❌ Error 4: Index Not Reset After Delete

After deleting rows, index may look messy.

βœ… Fix:

df = df.reset_index(drop=True)

πŸ’‘ Best Practices

  • Always use loc[] for safe updates
  • Check column names before operations
  • Reset index after deleting rows
  • Test code on small data first

πŸ‘‰ Learn Python basics here: Python Practice Questions

⚠️ Common Errors in Pandas (And How to Fix Them)

Fix common mistakes while inserting, updating, and deleting rows

❌ Error 1: SettingWithCopyWarning

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

❌ Error 2: KeyError (Column Not Found)

This error occurs when the column name is incorrect or misspelled.

# Wrong
df["age"]

βœ… Fix: Check column names:

print(df.columns)

❌ Error 3: ValueError (Length Mismatch)

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]

❌ Error 4: Index Not Reset After Delete

After deleting rows, index may look messy.

βœ… Fix:

df = df.reset_index(drop=True)

πŸ’‘ Best Practices

  • Always use loc[] for safe updates
  • Check column names before operations
  • Reset index after deleting rows
  • Test code on small data first

πŸ‘‰ Learn Python basics here: Python Practice Questions

πŸš€ Mini Project: Customer Data Analysis Using Pandas

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.

πŸ“Š Step 1: Create Dataset

Create a dataset with customer names and ages.

Example: Create a table with Name and Age columns.

βž• Step 2: Insert New Customer

Add a new customer record to your dataset.

Use: loc method to insert new row

✏️ Step 3: Update Customer Data

Modify existing values such as age or name.

Use: loc method for updating values

πŸ—‘οΈ Step 4: Delete Customer

Remove a customer record if needed.

Use: drop method

🎯 Final Output

After performing all operations, your dataset will be updated with new entries, corrected values, and removed records.

πŸ’‘ Project Tip

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

πŸ“₯ Download Practice Files & Notes

Practice Pandas operations with real data

To master Pandas insert, update, and delete operations, download the practice dataset and try it yourself.

πŸ“Š Sample Dataset

Practice with customer and product data

πŸ“ Notes PDF

Quick revision notes for Pandas operations

πŸ’» Practice Questions

Test your knowledge with exercises

πŸš€ Start Practicing Now

Practicing on real datasets is the fastest way to learn data analytics.

πŸ‘‰ Explore full course: Data Analytics Course

🧠 Test Your Knowledge (Pandas MCQ)

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?




Student Success Stories – Vista Alumni Achievements

Real students. Real transformations. From beginners to professionals.

πŸŽ“ Anjali Verma
Commerce β†’ Data Analyst at Accenture.
πŸ‘¨β€πŸ’» Rohit Rawat
BPO β†’ BI Executive (Python + Power BI).
πŸ“Š Meena Joshi
Career switch β†’ Remote Data Consultant.
πŸ‘¨β€πŸ« Chandresh Aggarwal
Now Faculty (Python & SQL) at Invertis University.
🏦 Siddharth Mall
Data Analyst at IndusInd Bank. SQL & dashboards expert.
πŸ€– Akash Singh
Junior Data Scientist at Capgemini.
πŸ“ˆ Taruna
Data Visualization Expert at RMSI (Power BI + GIS).
🏭 Ramandeep Singh
B.Com β†’ MIS Executive at Ambuja Cement (MNC).
πŸ’Ό Abhishek
Python Automation β†’ Clarivoyance IT Pvt. Ltd.
🌟 Asfi Mahim
Bijnor β†’ Junior Data Analyst at Guardian One Brands.

πŸ… Vista Academy Gallery & Certificates

Vista Certified Data Analyst Program

πŸŽ“ Every certificate is a story of transformation and success.

πŸš€ Start Your Data Career Today

Join Vista Academy & become job-ready for top companies & government roles.

Join Now

Ready to Start Your Business Analytics Career?

Learn industry tools. Build real dashboards. Crack interviews. Join Vista Academy – Dehradun’s trusted Data & Business Analytics institute.

Next Batch
Limited Seats
Small batches ensure personal mentorship.
Mode
Offline + Live Online
Classroom in Dehradun & remote across Uttarakhand.