pandas
dataframe
drop rows
python
data manipulation

How to drop a list of rows from Pandas dataframe?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Dropping multiple rows from a pandas DataFrame is usually straightforward, but the right syntax depends on whether you are dropping by index label, by position, or by a condition. Most confusion comes from mixing up row labels with row numbers.

Drop Rows by Index Label

If you already know the index labels you want to remove, pass them to drop.

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {
5        "name": ["Alice", "Bob", "Charlie", "David"],
6        "age": [24, 27, 22, 32],
7    },
8    index=[10, 11, 12, 13],
9)
10
11result = df.drop([11, 13])
12print(result)

This removes the rows whose index labels are 11 and 13.

Do Not Confuse Labels With Positions

This matters a lot. In pandas, drop([1, 3]) means "drop index labels 1 and 3," not necessarily "drop the second and fourth rows."

If your index is not a simple zero-based range, the result can surprise you.

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {"value": [100, 200, 300]},
5    index=[101, 102, 103]
6)
7
8# Drops label 102, not position 1
9print(df.drop([102]))

Knowing whether you are working with labels or positions is the key to avoiding errors.

Drop Rows by Position

If you have row positions rather than labels, convert those positions into index labels first.

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {"name": ["Alice", "Bob", "Charlie", "David"]}
5)
6
7positions = [1, 3]
8result = df.drop(df.index[positions])
9print(result)

This is the standard way to remove rows by row-number position.

Use errors='ignore' When Some Labels May Be Missing

By default, drop raises an error if a requested label is not present. If that is expected in your workflow, use errors="ignore".

python
1import pandas as pd
2
3df = pd.DataFrame({"x": [1, 2, 3]})
4result = df.drop([1, 99], errors="ignore")
5print(result)

This keeps the operation safe when the label list is coming from unreliable external input.

Reassign or Use inplace=True

Most pandas code is clearer when you assign the result back to a variable.

python
df = df.drop([1, 3])

You may also see:

python
df.drop([1, 3], inplace=True)

Both work, but reassignment is often easier to reason about and easier to use in method chains.

Drop Rows by Condition Instead

Sometimes a list of rows is really a condition in disguise. In those cases, boolean filtering is cleaner than building an explicit drop list.

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {
5        "name": ["Alice", "Bob", "Charlie", "David"],
6        "age": [24, 27, 22, 32],
7    }
8)
9
10result = df[df["age"] >= 25]
11print(result)

That keeps the logic close to the data rule instead of translating it into a list of indexes first.

Reset the Index If Needed

After dropping rows, the remaining index values stay as they were unless you reset them.

python
1import pandas as pd
2
3df = pd.DataFrame({"name": ["Alice", "Bob", "Charlie", "David"]})
4result = df.drop([1, 3]).reset_index(drop=True)
5print(result)

This is useful when the old index no longer has meaning and you want a clean sequential index again.

Common Pitfalls

The biggest pitfall is assuming drop([1, 3]) refers to row positions when it actually refers to index labels. That mistake is easy to make once the index stops being 0, 1, 2, ....

Another issue is using inplace=True everywhere even when reassignment would be clearer and easier to debug.

Developers also forget that drop returns a new DataFrame unless inplace=True is used. If you call df.drop(...) and ignore the result, nothing changes.

Finally, if your drop list may contain unknown labels, either validate it first or use errors="ignore" to avoid noisy exceptions.

Summary

  • Use df.drop([...]) when you want to remove rows by index label.
  • Use df.drop(df.index[positions]) when you want to remove rows by position.
  • Reassign the result unless you have a strong reason to use inplace=True.
  • Use boolean filtering when the real rule is conditional rather than a fixed list.
  • Reset the index afterward if the old labels no longer make sense.

Course illustration
Course illustration

All Rights Reserved.