我希望使用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中对虚拟列使用条件?
发布于 2022-01-05 15:22:08
要做到这一点,最好的方法可能是使用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)发布于 2021-12-30 05:57:56
使用掩码来对相关行进行细分可能是有用的:
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请注意,在最初的脚本中,numpy会因为取0的np.log而触发错误,所以在这种情况下使用np.log1p可能更合适。
https://stackoverflow.com/questions/70513644
复制相似问题