Home > Back-end >  Pandas: Remove rows where all values equal a certain value
Pandas: Remove rows where all values equal a certain value

Time:03-23

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.

CodePudding user response:

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