Xavier DIAZ ORTIZ
Xavier DIAZ ORTIZ

Reputation: 11

How to eliminate all columns with specific column name except for one particular variable/column?

I eliminate all columns on my df that contain "OBJ" in their respective colnames with the following code:

d = pd.read_csv(url) 
df = d[d.columns.drop(list(d.filter(regex='OBJ')))]

I do want to eliminate all variables which colnames contain the character "OBJ", except for a specific variable called "REV_OBJ".

Is there a way I can eliminate all "OBJ" except for "REV_OBJ"?

Upvotes: 1

Views: 55

Answers (2)

Deepak
Deepak

Reputation: 707

You can use list comprehension for the same, and can have one set which contains all the elements whom to remove.

to_remove = set([col for col in df.columns if "obj" in col.lower()]) - {'REV_OBJ'}
df.drop(to_remove, axis = 1, inplace = True)

Upvotes: 0

Ji Wei
Ji Wei

Reputation: 881

(personal preference) this is more readable:

cols = [col for col in df.columns if not 'OBJ' in col or col=='REV_OBJ']

Upvotes: 1

Related Questions