簡體   English   中英

如果條件為真,則在 Pandas 中創建一個新列並從現有列中分配值

[英]create a new column in pandas and assign value from existing column if condition is true

我有 15 分鍾的股票數據蠟燭圖並且有一個空頭信號 - 如果信號 = 0,我想創建一個新的列止損,然后止損 = 第二根蠟燭的高點,即( df['high'].shift( -2) )

                        open     high      low    close  signal
date                                                           
2020-01-01 09:15:00  1452.50  1457.00  1449.20  1452.50     NaN
2020-01-01 09:30:00  1452.30  1454.40  1450.00  1451.45     NaN
2020-01-01 09:45:00  1450.50  1454.80  1450.00  1453.75     NaN
2020-01-01 10:00:00  1453.70  1453.70  1450.10  1450.70     0.0
2020-01-01 10:15:00  1450.70  1453.00  1450.50  1452.20     NaN
2020-01-01 10:30:00  1452.00  1452.00  1446.75  1446.85     NaN
2020-01-01 10:45:00  1447.60  1449.00  1445.50  1447.10     NaN
2020-01-01 11:00:00  1446.75  1449.00  1446.55  1447.65     NaN

在這個例子中:2020-01-01 10:00:00 的空頭信號的止損將是 1452.00
這是 2020-01-01 10:30:00 的高點

讓我們試試np.where(condition, answer if condition is true, answer if condition is false)

df['stop-loss']=np.where(df.signal==0,df.high.shift(-2),'')

在這種情況下,您沒有指定如果為假則條件應該是什么,所以我把它放在那里''

                        open    high      low    close  signal stop-loss
date                                                                    
2020-01-01 09:15:00  1452.50  1457.0  1449.20  1452.50     NaN          
2020-01-01 09:30:00  1452.30  1454.4  1450.00  1451.45     NaN          
2020-01-01 09:45:00  1450.50  1454.8  1450.00  1453.75     NaN          
2020-01-01 10:00:00  1453.70  1453.7  1450.10  1450.70     0.0    1452.0
2020-01-01 10:15:00  1450.70  1453.0  1450.50  1452.20     NaN          
2020-01-01 10:30:00  1452.00  1452.0  1446.75  1446.85     NaN          
2020-01-01 10:45:00  1447.60  1449.0  1445.50  1447.10     NaN          
2020-01-01 11:00:00  1446.75  1449.0  1446.55  1447.65     NaN          

在評論中遵循您的其他問題。 假設數據幀是

                  open    high      low    close  signal
date                                                          
2020-01-01 09:15:00  1452.50  1457.0  1449.20  1452.50     NaN
2020-01-01 09:30:00  1452.30  1454.4  1450.00  1451.45     NaN
2020-01-01 09:45:00  1450.50  1454.8  1450.00  1453.75     NaN
2020-01-01 10:00:00  1453.70  1453.7  1450.10  1450.70     0.0
2020-01-01 10:15:00  1450.70  1453.0  1450.50  1452.20     NaN
2020-01-01 10:30:00  1452.00  1452.0  1446.75  1446.85     1.0
2020-01-01 10:45:00  1447.60  1449.0  1445.50  1447.10     NaN
2020-01-01 11:00:00  1446.75  1449.0  1446.55  1447.65     NaN

使用np.select([conditons],[choices], alternative)

conditions=[df.signal==0,df.signal==1]
choices=[df.high.shift(-2),df.low.shift(-2)]
df['stop-loss']=np.select(conditions, choices,'')



                   open    high      low    close  signal stop-loss
date                                                                    
2020-01-01 09:15:00  1452.50  1457.0  1449.20  1452.50     NaN          
2020-01-01 09:30:00  1452.30  1454.4  1450.00  1451.45     NaN          
2020-01-01 09:45:00  1450.50  1454.8  1450.00  1453.75     NaN          
2020-01-01 10:00:00  1453.70  1453.7  1450.10  1450.70     0.0    1452.0
2020-01-01 10:15:00  1450.70  1453.0  1450.50  1452.20     NaN          
2020-01-01 10:30:00  1452.00  1452.0  1446.75  1446.85     1.0   1446.55
2020-01-01 10:45:00  1447.60  1449.0  1445.50  1447.10     NaN          
2020-01-01 11:00:00  1446.75  1449.0  1446.55  1447.65     NaN        

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM