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

Pandas: Remove rows where all values equal a certain value

I have a DataFrame with regex search results. I need to remove any row where there were no matches for any of the terms. Not all columns are search results, only columns 2 – 6.

Have tried ( NF = "Not Found" ):

cond1 = (df['term1'] != "NF") & (df['term2'] != "NF") & (df['term3'] != "NF") & (df['term4'] != "NF") & (df['term5'] != "NF")
df_pos_results = df[cond1]

For some reason this is removing positive results.

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 :

I think you need .all:

df = df[df.iloc[:, 1:5].ne('NF').all(axis=1)]

That will remove all rows where every value in the row is equal to NF.

For multiple values:

df = df[~df.iloc[:, 1:5].isin(['NF', 'ABC', 'DEF']).all(axis=1)]
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