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?

1
  • 2
    There is a little known method that does this: df[df.columns.difference('col_n')]. Commented May 25, 2016 at 15:09

2 Answers 2

11

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).*)$')
Sign up to request clarification or add additional context in comments.

2 Comments

There is also a little known method that does this: df[df.columns.difference('col_n')].
@IanS, thanks! I've added it to the answer
1

You can use df.drop:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.