使用 max() 函数的 Dataframe 列值

Dataframe column value using max() function

我正在尝试创建一个名为 "Threshold" 的列,其中的值由计算 df['column']/30**0.5 确定,但我希望此列的最小值为 0.2。因此,如果计算结果低于 0.2,我希望列值为 0.2。

例如: df['column2'] = (df['column']/30)**0.5 或 0.2(取大者)。

这是我目前拥有的:

df['Historical_MovingAverage_15'] = df['Historical_Average'].rolling(window=15).mean()
df['Threshold'] = max((((df['Historical_MovingAverage_15'])/30)**0.5), 0.2)

它给我这个错误:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

使用numpy.maximum:

df['Threshold'] = np.maximum((((df['Historical_MovingAverage_15'])/30)**0.5), 0.2)

Series.cliplower 参数:

df['Threshold'] = (((df['Historical_MovingAverage_15'])/30)**0.5).clip(lower=0.2)

示例:

df = pd.DataFrame({'Historical_MovingAverage_15':[.21,2,3]})
df['Threshold'] = np.maximum((((df['Historical_MovingAverage_15'])/30)**0.5), 0.2)
print (df)
   Historical_MovingAverage_15  Threshold
0                         0.21   0.200000
1                         2.00   0.258199
2                         3.00   0.316228

详情:

print ((((df['Historical_MovingAverage_15'])/30)**0.5))
0    0.083666
1    0.258199
2    0.316228
Name: Historical_MovingAverage_15, dtype: float64