Reputation: 11
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
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
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