使用max()函数的Dataframe列值

2024-05-18 07:14:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个名为“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().

Tags: dfthreshold错误column数字windowmeanmax
1条回答
网友
1楼 · 发布于 2024-05-18 07:14:19

使用^{}

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

或带有lower参数的^{}

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

相关问题 更多 >