Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select columns from pandas dataframe that do not start with X

I’m trying to subset a dataframe by selecting all columns that do not 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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading