[英]How can I compare columns of a pandas dataframe?
I have the following df relevant_bars
:我有以下 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现在我想打印 ("true") if
Since I'm not deep into pd I tried to accomplish this by a plain vanilla python logic setup.由于我对 pd 没有深入了解,因此我尝试通过普通的 python 逻辑设置来完成此操作。 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 线程中读到的那样,您不应该将迭代与 pd 数据帧结合使用。 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")
..
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
. (df.col0>df.col1).all()
检查col0
中的所有值是否大于col1
。 (col2 > mean).all()
checks if all values in col2
are greter than some value mean
. (col2 > mean).all()
检查col2
中的所有值是否都大于某个值mean
。 Iff both are true, res
will be True
.如果两者都为真,
res
将为True
。
Try:尝试:
a = (relevant_bars['open'] < relevant_bars['close']) & (relevant_bars['volume'] > volume_mean)
a.all()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.