'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.



Solution 1:[1]

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)]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1