6

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
  • 732,580
  • 175
  • 1,330
  • 1,459
JuanPablo
  • 23,792
  • 39
  • 118
  • 164
  • 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

12

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 - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
1

You can use df.drop:

df.drop('column_one',axis=1)
Jan van der Vegt
  • 1,471
  • 12
  • 34