I'm trying to subset a dataframe by not selecting columns that I do not want that all start with the same phrase. In other words I want the entire dataframe, except for columns that start with "death".
Say I have a dataframe with these columns
player | birthDay | birthMonth | birthYear | deathDay | deathMonth | deathYear |
---|
I've created a filter list for columns I don't want to select
cols = [col for col in df if col.startswith("death")]
df[~cols]
But when I run this I get the error: bad operand type for unary ~: 'list'
How come I am receiving this error? And is there a better way of doing this?
You can just reverse the logic to get a positive column list instead of a negative one:
cols = [col for col in df if not col.startswith("death")]
df[cols]