How to select all columns except one in pandas?
Rule out a column in Pandas, using .drop
with the column's name and 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
:
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:
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:
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:
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()
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:
Much like a marauder's map of columns you don't want to bump into.
Was this article helpful?