Drop multiple columns in pandas

Question:

I am trying to drop multiple columns (column 2 and 70 in my data set, indexed as 1 and 69 respectively) by index number in a pandas data frame with the following code:

df.drop([df.columns[[1, 69]]], axis=1, inplace=True)

I get the following error:

TypeError: unhashable type: 'Index'

And in my code the [1, 69] is highlighted and says:

Expected type 'Integral', got 'list[int]' instead

The following code does what I want in two lines of repetitive code (first dropping col index 69, then 1, and order does matter because dropping earlier columns changes the index of later columns).

df.drop([df.columns[69]], axis=1, inplace=True)
df.drop([df.columns[1]], axis=1, inplace=True)

Is there a way to do this in one line similar to the first code snippet above?

Asked By: lukewitmer

||

Answers:

You don’t need to wrap it in a list with [..], just provide the subselection of the columns index:

df.drop(df.columns[[1, 69]], axis=1, inplace=True)

as the index object is already regarded as list-like.

Answered By: joris

Try this

df.drop(df.iloc[:, 1:69], inplace=True, axis=1)

This works for me

Answered By: Okroshiashvili

A readable version is to pass the columns= argument.

df = df.drop(columns=df.columns[[1, 69]])

Putting df.columns[[1, 69]] inside a list (as in OP) becomes useful if we want to drop columns both by integer position and name(s) (simply need to unpack the array with *). For example, the following code drops the 2nd and the 70th columns along with another column named Some_Col.

df = df.drop(columns=[*df.columns[[1, 69]], 'Some_Col'])

Another way to drop columns is via the iloc indexer. For example, to drop the 2nd and 70th columns:

df = df.iloc[:, np.r_[:1, 2:69, 70:df.shape[1]]]
# or
df = df.iloc[:, ~np.isin(np.arange(df.shape[1]), [1,69])]
Answered By: cottontail
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.