4

In pandas, if I have a dataframe called df, I can get one column with

df.column_one

and, I can get some specific columns with

df[['column_one', 'column_two']]

how I can get all columns without one specific?

Example: if I have a dataframe with n columns col_1, col_2, ... col_n, How I can get all columns without col_n?

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
JuanPablo
  • 22,464
  • 33
  • 113
  • 159
  • 2
    There is a little known method that does this: `df[df.columns.difference('col_n')]`. – IanS May 25 '16 at 15:09

2 Answers2

10

try this:

df.drop(['col_n'], axis=1)

or

df.loc[:, df.columns != 'col_n']

or

df.loc[:, df.columns - ['col_n']]

or as @IanS posted in the comment:

df[df.columns.difference('col_n')]

or using filter() function in junction with negative look ahead RegEx:

df.filter(regex=r'^((?!col_n).*)$')
MaxU - stop genocide of UA
  • 191,778
  • 30
  • 340
  • 375
0

You can use df.drop:

df.drop('column_one',axis=1)
Jan van der Vegt
  • 1,391
  • 9
  • 32