Home > Enterprise >  Change entire row to nan if condition in 3 columns met
Change entire row to nan if condition in 3 columns met

Time:03-01

I have a data frame with 400 columns of frequency data and 3 columns with whether pumps 1 -> 3 are "RUNNING" or "STOPPED".

I want to change entire rows to nan where a condition on pump is met. (It doesn't matter if this changes the pump rows as I drop those columns after this)

For example I've tried

df[(df["Pump Set 1"] != "STOPPED") & (df["Pump Set 2"] != "STOPPED") & (df["Pump Set 3"] != "STOPPED")] = None

When I do this and print df.head(), I see no change but if I try and to

df = df[etc

I get a none type error. All other examples I've seen online require specifying the columns to change and I don't want to specify 400 columns.

CodePudding user response:

You can use DataFrame.loc with : for all columns:

mask = (df["Pump Set 1"] != "STOPPED") & 
       (df["Pump Set 2"] != "STOPPED") & 
       (df["Pump Set 3"] != "STOPPED")
df.loc[mask, :] = None

Or pass all columns:

df.loc[mask, df.columns] = None
  • Related