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.
Vista Academy โ€“ 316/336, Park Rd, Laxman Chowk, Dehradun โ€“ 248001
๐Ÿ“ž +91 94117 78145 | ๐Ÿ“ง thevistaacademy@gmail.com | ๐Ÿ’ฌ WhatsApp
๐Ÿ’ฌ Chat on WhatsApp: Ask About Our Courses