lebca
lebca

Reputation: 185

Pandas: Subset a data frame by column names NOT wanted

To subset a dataframe by column names I usually do:

df[['A', 'B']]

where list(df.columns.values) = ['A', 'B', 'C', 'D']

Say I instead wanted to get all the columns except 'B'. How would I do this? This apparently does not work:

df[!['B']]

Upvotes: 3

Views: 2704

Answers (2)

Grr
Grr

Reputation: 16079

You can use ix to achieve this by requesting the rows you want (in this case all) and the columns you want (or dont). For example:

df.ix [:, df.columns != 'B']

Would get you all rows for all columns except 'B'

Upvotes: 0

unutbu
unutbu

Reputation: 879471

Use the drop method:

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

drop can also accept a list of columns, if you wish to drop more than one.

Upvotes: 2

Related Questions