Q:

If both values in two different columns are NaN then 0 else 1

My dataframe looks lie this:
ts  self_top_ask_price  self_top_bid_price
0   2020-05-03 11:59:48.627436  NaN 0.08331
1   2020-05-03 11:59:36.286763  0.08367 0.08331
2   2020-05-03 11:59:24.279036  0.08367 NaN
3   2020-05-03 11:59:12.298755  NaN NaN
What I am trying to achieve is that if both columns are NaN then in a third column I have the value 0 for that row otherwise 1.
So the output should be like the following:
ts  self_top_ask_price  self_top_bid_price both
0   2020-05-03 11:59:48.627436  NaN 0.08331  1
1   2020-05-03 11:59:36.286763  0.08367 0.08331 1
2   2020-05-03 11:59:24.279036  0.08367 NaN 1
3   2020-05-03 11:59:12.298755  NaN NaN 0

I tried the following but it does not work any idea?
metrics[['self_top_ask_price', 'self_top_bid_price']] = metrics['both'].applymap(lambda x: 0 if pd.isnull(x) else 1)

Thanks!!

A:

Check both columns if not missing values with DataFrame.any and then convert to numbers 0,1 mapping boolean by Series.astype:
metrics['both'] = (metrics[['self_top_ask_price', 'self_top_bid_price']].notna()
                                                          .any(axis=1)
                                                          .astype(int))

Or by Series.view:
metrics['both'] = (metrics[['self_top_ask_price', 'self_top_bid_price']].notna()
                                                          .any(axis=1)
                                                          .view('i1'))

print (metrics)
                           ts  self_top_ask_price  self_top_bid_price  both
0  2020-05-03 11:59:48.627436                 NaN             0.08331     1
1  2020-05-03 11:59:36.286763             0.08367             0.08331     1
2  2020-05-03 11:59:24.279036             0.08367                 NaN     1
3  2020-05-03 11:59:12.298755                 NaN                 NaN     0

