我有一个股票数据框。和
要运行许多if
语句来分配值。
c1 = np.where((blackCondition & whitePCondition) & (close_gt_oP & open_ge_cP),'UP',None)
c2 = np.where((blackCondition & whitePCondition) & (open_lt_cP & close_le_oP),'DOWN',None)
c3 = np.where((blackCondition & whitePCondition) & (close_gt_oP & open_lt_cP),'OUTSIDE',None)
c4 = np.where((blackCondition & whitePCondition) & (close_le_oP & open_ge_cP),'INSIDE',None)
#IF2
c5 = np.where((blackCondition & blackPCondition) & (open_gt_oP & close_ge_cP),'UP',None)
c6 = np.where((blackCondition & blackPCondition) & (close_lt_cP & open_le_oP),'DOWN',None)
c7 = np.where((blackCondition & blackPCondition) & (open_gt_oP & close_lt_cP),'OUTSIDE',None)
c8 = np.where((blackCondition & blackPCondition) & (open_le_oP & close_ge_cP),'INSIDE',None)
#IF3
c9 = np.where((whiteCondition & whitePCondition) & (close_gt_cP & open_ge_oP),'UP',None)
c10 = np.where((whiteCondition & whitePCondition) & (open_lt_oP & close_le_cP),'DOWN',None)
c11 = np.where((whiteCondition & whitePCondition) & (close_gt_cP & open_lt_oP),'OUTSIDE',None)
c12 = np.where((whiteCondition & whitePCondition) & (close_le_cP & open_ge_oP),'INSIDE',None)
#IF4
c13 = np.where((whiteCondition & blackPCondition) & (open_gt_cP & close_ge_oP),'UP',None)
c14 = np.where((whiteCondition & blackPCondition) & (close_lt_oP & open_le_cP),'DOWN',None)
c15 = np.where((whiteCondition & blackPCondition) & (open_gt_cP & close_lt_oP),'OUTSIDE',None)
c16 = np.where((whiteCondition & blackPCondition) & (open_le_cP & close_ge_oP),'INSIDE',None)
如何将语句的TRUE VALUE赋值给列?
df [' pos'] =如果本声明的任何内容是正确的
例如:
if(c1) => df['pos'] = TRUE_CONDITION_OF_C1
答案 0 :(得分:1)
对于大熊猫,我不知道这样做的一种非常优雅的方式。但有几个选择。
您可以重复使用.map()
一个丑陋的面具,或者您可以使用.apply()
或df['pos'] = (
(blackCondition & whitePCondition)
& (close_gt_oP & open_ge_cP)
).map({True: "UP", False: "DOWN"})
方法。
以下示例假设掩码无效,且与df本身具有相同的形状。
// Create a file named index.mjs (the extension is the key)
import axios from 'axios';
console.log(axios);
那只会做第一行。
答案 1 :(得分:1)
我认为条件设置值需要numpy.select
,我也尝试将相同条件重新分配给变量m1-m3
以获得更好的效果:
m1 = (blackCondition & whitePCondition)
m2 = (blackCondition & blackPCondition)
m3 = (whiteCondition & whitePCondition)
#same like m1
#m4 = (whitePCondition & blackCondition)
c1 = m1 & (close_gt_oP & open_ge_cP)
c2 = m1 & (open_lt_cP & close_le_oP)
c5 = m2 & (open_gt_oP & close_ge_cP)
c6 = m2 & (close_lt_cP & open_le_oP)
c9 = m3 & (close_gt_cP & open_ge_oP)
c10 = m3 & (open_lt_oP & close_le_cP)
c13 = m1 & (open_gt_cP & close_ge_oP)
c14 = m1 & (close_lt_oP & open_le_cP)
c3 = m1 & (close_gt_oP & open_lt_cP)
c4 = m1 & (close_le_oP & open_ge_cP)
c7 = m2 & (open_gt_oP & close_lt_cP)
c8 = m2 & (open_le_oP & close_ge_cP)
c11 = m3 & (close_gt_cP & open_lt_oP)
c12 = m3 & (close_le_cP & open_ge_oP)
c15 = m1 & (open_gt_cP & close_lt_oP)
c16 = m1 & (open_le_cP & close_ge_oP)
|
OR
的链条件:
cup = c1 | c5 | c9 | c13
cdown = c2 | c6 | c10 | c14
coutside = c3 | c7 | c11 | c15
cinside = c4 | c8 | c12 | c16
df['pos'] = np.select([cup, cdown, coutside, cinside],
['UP','DOWN','OUTSIDE','INSIDE'], default=None)