Pandasdrill

Selecting and filtering Updated November 2026

pandas drop

df.drop(columns=["a", "b"]) returns a copy without those columns, and df.drop(index=[0, 2]) returns a copy without those rows. Nothing changes in place, so assign the result back: df = df.drop(columns=["a"]). To remove rows by a condition, keep the rows you want with a boolean filter instead.

Syntax

df.drop(labels=None, axis=0, index=None, columns=None, errors="raise")
PartWhat it is
columnsColumn name or list of names to remove
indexRow label or list of labels to remove
errors"ignore" skips labels that do not exist instead of raising KeyError

Examples

import pandas as pd

df = pd.DataFrame({"name": ["Ana", "Ben", "Cy"],
                   "dept": ["Ops", "IT", "IT"],
                   "salary": [52, 61, 58],
                   "notes": ["", "x", ""]})
df.drop(columns=["notes"])

Result:

  name dept  salary
0  Ana  Ops      52
1  Ben   IT      61
2   Cy   IT      58

The original df still has the notes column until you assign the result.

df.drop(index=[0, 2])

Result:

  name dept  salary notes
1  Ben   IT      61     x

Rows are removed by index label, not by position.

df[df["dept"] != "IT"]

Result:

  name dept  salary notes
0  Ana  Ops      52      

Dropping rows by a condition is a filter: keep everything that is not IT.

df.drop(columns=["bonus"], errors="ignore").columns.tolist()

Result: ['name', 'dept', 'salary', 'notes']

There is no bonus column; errors="ignore" returns the frame unchanged instead of failing.

Try it yourself

Return emp without the emp_id and city columns. Keep every row and the other columns in their original order.

emp: 8 rows, first 4 shown

emp_idnamedeptsalarycityyears
1"Ava Chen""Sales"62000"Boston"3
2"Ben Ortiz""Engineering"98000"Austin"6
3"Cara Patel""Marketing"71000"Boston"4
4"Dev Singh""Engineering"105000"Denver"9

Free, no account needed. Real pandas runs in your browser; the first run takes a few seconds to load Python.

Common mistakes

  • KeyError: "['x'] not found in axis": the label does not exist, or you passed a column name without columns=, so pandas looked for a row with that label.
  • Nothing seems to happen: drop returns a new DataFrame. Write df = df.drop(...) or the change is lost.
  • Dropping rows by position: drop uses labels. After filtering or sorting the labels are no longer 0, 1, 2; use df.drop(df.index[[0, 2]]) or reset_index(drop=True) first.

More practice: drop

These drills check your answer on live data, free, no account needed.

  • Exclude a group with ~Return the employees who are not in Engineering and have at least 3 years of service. Keep all columns and the original row labels.

Questions

How do I drop a column in pandas?

df = df.drop(columns=["col"]). For several, pass a list: df.drop(columns=["a", "b"]).

How do I drop rows that have missing values?

That is dropna, not drop: df.dropna() removes rows with any NaN, df.dropna(subset=["col"]) only looks at one column.

How do I keep only some columns instead?

Select them: df[["name", "salary"]]. It is clearer than dropping everything else.

Related

  • dropnaHow to drop NaN rows in pandas with dropna: any or all missing, only certain columns with subset, drop columns with axis=1, thresh for a minimum of values.
  • drop_duplicatesHow to remove duplicates in pandas with drop_duplicates: whole rows or a subset of columns, keep first, last or none, and finding duplicates with duplicated.
  • Filter rowsHow to filter rows in a pandas DataFrame by one or several conditions using boolean masks, with the & | ~ operators, parentheses, isin, between and query.
  • loc vs ilocThe difference between loc and iloc in pandas: loc selects by index label and column name, iloc by integer position. Slices, lists and boolean masks.

Previous: explodeNext: isna and notna