Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

How to Drop Columns in pandas: drop(), dropna() and pop()

Drop columns in pandas with drop(columns=), dropna(axis=1) or pop(), fix the KeyError and avoid deprecated inplace=True. Checked against the pandas 3.1.0 docs.

Written and maintained by CASRAI Editorial Board

Last updated

To drop columns in pandas, call DataFrame.drop with the columns argument and assign the result to a variable: df = df.drop(columns=['B', 'C']). It returns a new DataFrame without those columns and leaves the original unchanged unless you ask otherwise. This guide covers drop(), dropna() for removing columns that contain missing values, and pop() for removing a column while keeping its values. The commands below are taken from the pandas 3.1.0 documentation, checked on 2026-10-29.

What it is used for in research

Analysis datasets often arrive with more columns than a study needs: identifiers that must not travel with the data, free-text fields, export artefacts, or variables outside the analysis plan. Dropping them is a routine step of data cleaning and data wrangling, and it usually comes early in an exploratory data analysis in Python. Because the operation changes which variables your results are based on, write it as an explicit line in your analysis script rather than deleting columns by hand in a spreadsheet, so anyone rerunning the script can see exactly what was removed.

Install or access

pandas is installed from the official package channels. The pandas installation page (pandas.pydata.org/docs/getting_started/install.html) lists:

  • pip install pandas
  • conda install -c conda-forge pandas

Then import it in your script or notebook with import pandas as pd.

Step-by-step: drop columns

The examples use the DataFrame from the pandas documentation for DataFrame.drop:

import numpy as np
import pandas as pd

df = pd.DataFrame(np.arange(12).reshape(3, 4), columns=['A', 'B', 'C', 'D'])

1. Drop one or several columns by name

  1. Pass the names to the columns argument: df.drop(columns=['B', 'C']).
  2. The documentation states that labels, axis=1 is equivalent to columns=labels, so df.drop(['B', 'C'], axis=1) gives the same result. The default is axis=0, which is why axis=1 (or 'columns') is needed when you use the positional form.
  3. Assign the result: df = df.drop(columns=['B', 'C']). The default behaviour of drop() is to return a copy.

For a single column, pass one name: df.drop(columns='B').

2. Drop rows and columns in one call

The signature accepts both index and columns, so one call can remove rows and columns together, for example df.drop(index='cow', columns='small') as in the documentation’s MultiIndex example.

3. Drop columns that contain missing values with dropna()

When the rule is "remove any column with missing data" rather than a list of names, use DataFrame.dropna with axis='columns' (or axis=1):

df = df.dropna(axis='columns')
  • how='any' (the default) drops a column if any value is missing; how='all' drops it only if every value is missing.
  • thresh sets the number of non-missing values a column needs to be kept. It cannot be combined with how.
  • subset restricts which labels are checked.

In the documentation’s example, df.dropna(axis='columns') leaves only the name column, because the toy and born columns each contain a missing value. Check how much data a rule would remove before you apply it; see NumPy NaN for how missing values are represented.

4. Remove a column and keep its values with pop()

DataFrame.pop(item) returns the item and drops it from the DataFrame. The returned object is the column as a Series, so it suits splitting a target variable from the features: y = df.pop('class'). Like drop(), it raises a KeyError if the column is not found.

Output and how to read it

With the example DataFrame, df.drop(['B', 'C'], axis=1) and df.drop(columns=['B', 'C']) both return a DataFrame with columns A and D and three rows:

Row A D
0 0 3
1 4 7
2 8 11

Check the result with df.columns or df.shape rather than assuming the call worked. If the columns are still there, you called drop() without assigning the returned DataFrame.

Troubleshooting

Why do I get a KeyError?

The documentation says drop() raises a KeyError if any of the labels is not found in the selected axis. Check spelling, capitalisation and stray whitespace in the names (print(df.columns.tolist())). If a column may legitimately be absent, for example when you rerun a script on a file with different exports, pass errors='ignore': the documentation says this suppresses the error and drops only the labels that exist. Leave the default (errors='raise') when a missing column would mean something is wrong, so the script fails loudly.

Why did my columns not disappear?

By default drop() returns a copy and the original DataFrame is unchanged. Assign the result back: df = df.drop(columns=[...]).

Why does inplace=True give me None?

With inplace=True, drop() modifies the DataFrame and returns None, so df = df.drop(columns=['B'], inplace=True) replaces your DataFrame with None. More importantly, the pandas 3.1.0 documentation lists the inplace parameter as deprecated since version 3.1.0 and to be removed in pandas 4.0. Use the assignment form df = df.drop(columns=[...]), which works in current versions and does not depend on the removed parameter.

Why is my tuple treated as one column?

The documentation states that a tuple passed as labels is used as a single label and not treated as an iterable. This matters with MultiIndex columns, where a tuple names one column. To drop several columns, pass a list.

Which method should I use?

You want to Use Source
Remove columns you can name df.drop(columns=[...]) pandas DataFrame.drop reference
Remove columns that contain missing values df.dropna(axis='columns'), optionally with how or thresh pandas DataFrame.dropna reference
Remove a column and keep it as a Series df.pop('name') pandas DataFrame.pop reference
Skip labels that may not exist df.drop(columns=[...], errors='ignore') pandas DataFrame.drop reference

If you need to change column names rather than remove them, see how to rename columns in a pandas DataFrame.

Free alternatives

The same operation in other open-source data tools, per their own documentation (checked 2026-10-29):

Tool Drop columns Documented behaviour
Polars (Python) df.drop('ham'), df.drop(['bar', 'ham']) or df.drop('foo', 'ham') Accepts column names or selectors; strict=True by default raises an exception if a name does not exist in the schema
R, dplyr select(!c(Sepal.Length, Petal.Length)) The ! operator negates a selection, so the listed columns are removed

FAQ

How do I drop multiple columns in pandas?

Pass a list to columns: df = df.drop(columns=['B', 'C']).

What is the difference between axis=1 and columns=?

None in result. The documentation states labels, axis=1 is equivalent to columns=labels; columns= is easier to read because it says what is being dropped.

Should I still use inplace=True?

No. It is documented as deprecated since pandas 3.1.0 and scheduled for removal in 4.0. Assign the returned DataFrame instead.

How do I drop columns with missing values?

Use df.dropna(axis='columns'), and add how='all' or thresh if dropping on any missing value is too aggressive.

How do I drop a column only if it exists?

Use df.drop(columns=['B'], errors='ignore').

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about How to Drop Columns in pandas: drop(), dropna() and pop()

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →