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

Drop Column in Pandas Dataframe based on Row Conidtion

I have the following pandas dataframe and I would like to drop any column that has 0-Flat in the "Trade" row. How would I go about doing that?

enter image description here

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

>Solution :

This should do the job

headers_to_drop=[header for header in df.columns if (df[header]=="0-Flat").any()]
df=df.drop(headers_to_drop,axis=1)

The code first loops over all the columns and checks if there is any columns with the string and drops the selected columns.

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