I have the following df relevant_bars
:
open high low close volume
timestamp
2021-12-31 00:00:00 00:00 205.79 205.79 205.79 205.79 1140
2021-12-31 12:00:00 00:00 206.25 206.25 206.25 206.25 1146
Now I want to print ("true") if
- each closing price is above the open price
- each volume is greater than 'volume_mean'
Since I'm not deep into pd I tried to accomplish this by a plain vanilla python logic setup. However, this doesn't really seem to be the way of doing this nor will this be efficient once the conditions will add up even more.
As I've read from other SO threads you shouldn't use iterations in combination with pd dataframes. So how would I approach this?
if relevant_bars.iloc[0]['open'] < relevant_bars.iloc[0]['close'] and
relevant_bars.iloc[0]['volume'] > volume_mean and
relevant_bars.iloc[1]['open'] < relevant_bars.iloc[1]['close'] and
relevant_bars.iloc[1]['volume'] > volume_mean:
print("true")
..
CodePudding user response:
If you have, for example:
df = pd.DataFrame({'col0':[1,2,3],
'col1':[4,5,6],
'col2':[7,8,9]})
You can do:
mean = 5
res = (df.col0>df.col1).all() and (col2 > mean).all()
(df.col0>df.col1).all()
check that all values in col0
are greater than col1
. (col2 > mean).all()
checks if all values in col2
are greter than some value mean
. Iff both are true, res
will be True
.
CodePudding user response:
Try:
a = (relevant_bars['open'] < relevant_bars['close']) & (relevant_bars['volume'] > volume_mean)
a.all()