2

Let's say I have a DataFrame with the following columns: TAG, ALIAS, COMMENT, TYPE.

Is there a way to drop all columns that are not COMMENT without having to type the following line?

df.drop(["TAG","ALIAS","TYPE"], 1)

Is there a way to type an if statement somewhere in there and drop anything that is not the column call e.g., COMMENT?

Tim Grant
  • 3,093
  • 3
  • 21
  • 28
LuisP
  • 33
  • 3
  • 1
    I've misread the question so I reopened. However, if you only want to select one column, why don't you just use `df['COMMENT']`? – ayhan May 04 '17 at 21:15

2 Answers2

3

If you know that you only want the column COMMENT, just for for

df = df['COMMENT']

If you are looking for various columns starting with COMMENT, say COMMENT1, COMMENT2 etc, you can use filter

df = df.filter(like = 'COMMENT')

As @piRsquared suggested, another method of selecting columns would be

df = df[['COMMENT']]

This would be especially needed if you want to select multiple columns

df = df[['COMMENT', 'COMMENT1']]
Vaishali
  • 35,413
  • 4
  • 48
  • 78
3

I changed my mind on adding an answer. This is obnoxious as what A-Za-z and ayhan have said makes way more sense...

... However, that doesn't stop me from posting this

df.drop(df.columns.difference(['COMMENT']), 1)
piRSquared
  • 265,629
  • 48
  • 427
  • 571