Reputation: 1629
I have a stock dataframe. And
have many if
statement to run to assign a value.
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)
How can I assign TRUE VALUE of a statement to a column?
df['pos'] = IF ANY OF THIS STATEMENT IS TRUE
ex:
if(c1) => df['pos'] = TRUE_CONDITION_OF_C1
Upvotes: 1
Views: 63
Reputation: 862406
I think need numpy.select
for set values by conditions, also I try reassign same conditions to variables m1-m3
for better performance:
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)
Chain conditions by |
for 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)
Upvotes: 1
Reputation: 150
With pandas I don't know of a very elegant way of doing this. There are several options though.
You can repetitively use .loc[]
with a mask, which is ugly, or you could use a .map()
or .apply()
method.
The following example assumes that the mask is nowhere null and that is has the same shape as the df itself.
df['pos'] = (
(blackCondition & whitePCondition)
& (close_gt_oP & open_ge_cP)
).map({True: "UP", False: "DOWN"})
That would only do the first line.
Upvotes: 1