Explain Codes LogoExplain Codes Logo

How to select all columns except one in pandas?

python
pandas
dataframe
best-practices
Anton ShumikhinbyAnton Shumikhin·Nov 12, 2024
TLDR

Rule out a column in Pandas, using .drop with the column's name and axis=1:

df = df.drop('unwanted_column', axis=1)

This returns a fresh DataFrame df, bereft of 'unwanted_column'. The magic lies in axis=1, implying column-wise action.

Targeted column exclusion strategies

Using difference method

To weed out specific columns, especially in dataframes larger than a Hogwarts enrollment scroll, use difference:

df = df[df.columns.difference(['Voldemort', 'Malfoy'])] # Because who likes the villains, right?

In this example, difference plays Quidditch to bring back all columns, except the snitches.

Inverting with ~

~, the honey badger of Pandas, simply doesn't care about the columns you want to exclude:

df = df.loc[:, ~df.columns.isin(['unwanted_column'])] # Mimics Dobby's "Master has given Dobby a column"!

isin concocts a boolean cauldron with True for the to-be-excluded columns. ~ heads off to Beauxbatons, returning everyone else.

Previewing columns with drop

A quick peak into your dataframe, with a twist of exclusion:

print(df[df.columns.drop('Dolores Umbridge')]) # Because who wants Dolores in their sight?

This beauty doesn't tamper with df, it merely vanishes the 'Dolores Umbridge' column from the view.

Sticking the exclusion with inplace

To slam the door permanently on the columns:

df.drop('unwanted_column', axis=1, inplace=True)

The original df has been Obliviated of the 'unwanted_column' - for good! Yes, just like removing a Horcrux.

Mastering important facets

Taming the SettingWithCopyWarning beast

A hair-raising side-effect of dabbling in exclusion is SettingWithCopyWarning. Often, you won't know if you took a bite off the original dataframe or merely its reflection. The antidote? .copy()

new_df = df.loc[:, ~df.columns.isin(['unwanted'])].copy() # Now, no mimblewimble!

Avoiding "The Cursed Child"

As your Python grows up, pandas phases out some old friends, with .ix being one example. Stick to the timeless classic: .loc and its younger sibling, .iloc.

Organising column burial

Brew yourself a potion of favourite columns to dispose of, and smoothly channel your curses:

cols_to_exclude = ['temp', 'auxiliary', 'unwanted'] df = df.loc[:, ~df.columns.isin(cols_to_exclude)]

Much like a marauder's map of columns you don't want to bump into.