0

How can I add a field that returns 1/0 if the value in any specified column in not NaN?

Example:

df = pd.DataFrame({'id': [1,2,3,4,5,6,7,8,9,10],
                   'val1': [2,2,np.nan,np.nan,np.nan,1,np.nan,np.nan,np.nan,2],
                   'val2': [7,0.2,5,8,np.nan,1,0,np.nan,1,1],
                  })
display(df)
mycols = ['val1', 'val2']
# if entry in mycols != np.nan, then df[row, 'countif'] =1; else 0

Desired output dataframe:

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
a11
  • 3,122
  • 4
  • 27
  • 66

2 Answers2

1

We do not need countif logic in pandas , try notna + any

df['out'] = df[['val1','val2']].notna().any(1).astype(int)
df
Out[381]: 
   id  val1  val2  out
0   1   2.0   7.0    1
1   2   2.0   0.2    1
2   3   NaN   5.0    1
3   4   NaN   8.0    1
4   5   NaN   NaN    0
5   6   1.0   1.0    1
6   7   NaN   0.0    1
7   8   NaN   NaN    0
8   9   NaN   1.0    1
9  10   2.0   1.0    1
BENY
  • 317,841
  • 20
  • 164
  • 234
  • 1
    I also just realized this will work, in case someone else has this question and wants multiple options: `df['countif'] = df[df[mycols] != np.nan].count(axis=1).apply(lambda x: 1 if x > 0 else 0)` – a11 Mar 18 '21 at 20:24
  • 1
    @a11 we should try to avoid apply , more detail see https://stackoverflow.com/questions/54432583/when-should-i-not-want-to-use-pandas-apply-in-my-code – BENY Mar 18 '21 at 20:25
0

Using iloc accessor filtre last two columns. Check if the sum of not NaNs in each row is more than zero. Convert resulting Boolean to integers.

 df['countif']=df.iloc[:,1:].notna().sum(1).gt(0).astype(int)



id  val1  val2  countif
0   1   2.0   7.0        1
1   2   2.0   0.2        1
2   3   NaN   5.0        1
3   4   NaN   8.0        1
4   5   NaN   NaN        0
5   6   1.0   1.0        1
6   7   NaN   0.0        1
7   8   NaN   NaN        0
8   9   NaN   1.0        1
9  10   2.0   1.0        1
wwnde
  • 26,119
  • 6
  • 18
  • 32