【问题标题】:If both values in two different columns are NaN then 0 else 1如果两个不同列中的两个值都是 NaN,则为 0,否则为 1
【发布时间】:2020-05-04 13:25:42
【问题描述】:

我的数据框看起来是这样的:

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

我想要实现的是,如果两列都是 NaN,那么在第三列中,该行的值为 0,否则为 1。

所以输出应该是这样的:

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

我尝试了以下,但它没有任何想法?

metrics[['self_top_ask_price', 'self_top_bid_price']] = metrics['both'].applymap(lambda x: 0 if pd.isnull(x) else 1)

谢谢!!

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    DataFrame.any检查两列如果没有缺失值,然后通过Series.astype转换为数字0,1映射布尔值:

    metrics['both'] = (metrics[['self_top_ask_price', 'self_top_bid_price']].notna()
                                                              .any(axis=1)
                                                              .astype(int))
    

    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
    

    【讨论】:

      猜你喜欢
      • 2018-11-02
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      • 1970-01-01
      相关资源
      最近更新 更多