Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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']]
like image 361
intdt Avatar asked Nov 27 '25 17:11

intdt


1 Answers

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.

like image 64
unutbu Avatar answered Nov 30 '25 21:11

unutbu