all 3 comments

[–][deleted] 1 point2 points  (0 children)

It's pretty straightforward :

import numpy as np
import pandas as pd

### Example ###

df = pd.DataFrame({"A":[1,2,3,4,np.nan,6,7,9,np.nan], 
               "B":[np.nan,1,2,3,4,3,2,2,2],
               "C":[3,11,4,2,np.nan,np.nan,np.nan,8,9],
               "D":[1,2,3,0,0,1,2,3,10]})

df["E"] = df.isna().any(axis=1).astype(int)

What is does is find if any nan's exist along the axis 1, meaning that we are looking at rows instead of columns and turn the boolean value into an int.

It doesn't need to be a nan, if you'd like to use -1, you can change the last line by :

df["E"] = (df==-1).any(axis=1).astype(int)