在 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),'')

在这种情况下,您没有指定如果条件为 false 应该是什么,所以我放在那里 ''

                        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