python - pandas DataFrame - comparing two columns based on condition being true -
i have following pandas data frame:
df = pd.dataframe({'open': [1.20443, 1.20438, 1.20464, 1.20443, 1.20443, 1.2049, 1.20444, 1.20443], 'high': [1.20447, 1.20467, 1.20497, 1.20446, 1.20447, 1.20505, 1.20446, 1.20446], 'low': [1.20429, 1.20436, 1.20464, 1.20439, 1.20429, 1.20350, 1.20441, 1.20439], 'close': [1.20438, 1.20466, 1.20486, 1.20439, 1.20438, 1.20497, 1.20446, 1.20439], 'support': [1.20346, 1.20346, 1.20361, 1.20363, 1.20362, 1.20364, 1.20367, 1.20360], 'br': [false, false, true, false, false, true, false, false]})
what want iterate through column br
until true
, compare row value of low
previous row value of support
, iterate through point unit low
less support
, return true
or false
if not happen, continue iterating br
point left off.
i.e. row 2 br = true
compare low
row 2 support
row 1 until low < support
in row 5, continue iterating br
row 3.
i can loops , if statements, large dataset , take long time hoping there optimized way pandas can handle this. have tried map , apply can not seem past need iterate on each row item item.
maybe np.where
.
df['answer'] = np.where((df['br'] == true) & (df['low'] < df['support']), true, np.where((df['br'] == true) & (df['high'] > 1.20504), true, false))
or might able combine 1 np.where
:
df['answer'] = np.where(((df['br'] == true) & (df['low'] < df['support'])) | (df['br'] == true) & (df['high'] > 1.20504), true, false)
Comments
Post a Comment