LuisP
LuisP

Reputation: 33

Python using drop() method?

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?

Upvotes: 2

Views: 846

Answers (2)

piRSquared
piRSquared

Reputation: 294506

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)

Upvotes: 2

Vaishali
Vaishali

Reputation: 38415

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']]

Upvotes: 3

Related Questions