【问题标题】:Virtual column with calculation in Vaex在 Vaex 中进行计算的虚拟列
【发布时间】:2022-01-05 23:22:08
【问题描述】:

我想使用 Vaex 中的另一列将虚拟列设置为计算。我需要在此计算中使用 if 语句。一般来说我想打电话

df['calculation_col'] = log(df['original_col']) if df['original_col'] == 0 else -4

然后我尝试在 Vaex 中运行计数功能:

hist = df.count(
        binby='calculation_col',
        limits=limits,
        shape=binnum,
        delay=True
    )

当我尝试执行此代码时,我收到错误 ValueError: zero-size array to reduction operation minimum which has no identity

如何在 Vaex 中为虚拟列使用条件?

【问题讨论】:

    标签: vaex


    【解决方案1】:

    可能最“vaex”的方法是使用where

    import vaex
    df = vaex.example()
    # The syntax is where(condition, if satisfied, else)
    df['calculated_col'] = df.func.where(df['x'] < 10, 0, -4)
    

    【讨论】:

      【解决方案2】:

      使用掩码对相关行进行子集化可能很有用:

      import vaex
      
      df = vaex.example()
      
      mask = df["id"] < 10
      
      df["new_col"] = mask * df["x"] + ~mask * (-4)
      
      print(df[['id', 'x', 'new_col']].head(4))
      # #    id          x    new_col
      # 0     0   1.23187     1.23187
      # 1    23  -0.163701   -4
      # 2    32  -2.12026    -4
      # 3     8   4.71559     4.71559
      

      请注意,在原始脚本中,由于将np.log 设为零,numpy 会触发错误,因此在这种情况下使用np.log1p 可能更合适。

      【讨论】:

        猜你喜欢
        • 2012-01-05
        • 2022-01-26
        • 1970-01-01
        • 1970-01-01
        • 2011-05-27
        • 2012-06-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多