![](/img/trans.png)
[英]Group the values using one column and return the one having max value in other column using pandas dataframe
[英]Dataframe column value using max() function
我正在尝试创建一个名为“阈值”的列,其中值由计算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)
或具有lower
参数的Series.clip
:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.