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?

1
  • 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']? Commented May 4, 2017 at 21:15

2 Answers 2

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

1 Comment

So I don't have add an answer df = df[['COMMENT']]
2

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.