Home > Software design >  np.where check if columns are null
np.where check if columns are null

Time:04-29

I'm trying to write an np.where with a condition that looks to see if all columns except the first is null. I know how to filter the dataframe with this condition:

x[x.iloc[:, 1:].isna().all(1)]

I would like to use this as a condition to say if all column values except the first are null, then change the value in column 1 to something.

x['col1'] = np.where(x[x.iloc[:, 1:].isna().all(1)], 1, 0)

But this doesn't work

CodePudding user response:

You can just pass the boolean condtion to np.where

x['col1'] = np.where(x.iloc[:, 1:].isna().all(1), 1, 0)

CodePudding user response:

I think this should work :

x['col1'] = np.where(x.iloc[:, 1:].isna(), 1, 0)

CodePudding user response:

Check assign after you get the condition

colcond = x.iloc[:, 1:].isna().all(1)

df.iloc[0, colcond] =  somevaluelist
  • Related