使用max()函数的数据帧列值



我正试图创建一个名为"阈值"的列,其中的值由计算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

最新更新