我想用底層邏輯創建一個條件列:如果信號為 1,我將 5 添加到當前值,如果它為負,我洗掉 5,如果它為 0,我什么也不做。使用 pandas 上的 .cumsum() 對我來說很清楚如何做到這一點,但問題是我想限制這一點,以防值達到某個帶寬(在本例中為 45-55)。在下面的示例中,一旦值達到 55,另一個正信號不會增加它,而是等到負信號。
有誰知道我如何在我的代碼中添加這個理論帶寬?
前:
| 信號 | 價值 |
|---|---|
| 1 | 50 |
| 0 | 50 |
| -1 | 50 |
| 1 | 50 |
| 0 | 50 |
| 1 | 50 |
| -1 | 50 |
后:
| 信號 | 價值 | 創造的價值 |
|---|---|---|
| 1 | 50 | 55 |
| 0 | 50 | 50 |
| -1 | 50 | 45 |
| 1 | 50 | 55 |
| 0 | 50 | 50 |
| 1 | 50 | 55 |
| -1 | 50 | 45 |
uj5u.com熱心網友回復:
您的描述似乎與提供的輸出不匹配。從描述中,這是我的理解:
df['value created'] = (df['value']
# add 5 to the current value,
# if its negative I remove 5
# if its 0 I do nothing
.add(df['signal'].mul(5))
# restrict this in case value hits a certain bandwidth
# (in this example 45-55)
.clip(lower=45, upper=55)
)
輸出:
signal value value created
0 1 50 55
1 0 50 50
2 -1 50 45
3 1 50 55
4 0 50 50
5 1 50 55
6 -1 50 45
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/456653.html
