简体   繁体   English

如何比较 pandas dataframe 的列?

[英]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

  • each closing price is above the open price每个收盘价都高于开盘价
  • each volume is greater than 'volume_mean'每个音量都大于“volume_mean”

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.

相关问题 Pandas Dataframe:如何比较一行的两列中的值是否等于后续行的同一列中的值? - Pandas Dataframe: how can i compare values in two columns of a row are equal to the ones in the same columns of a subsequent row? 如何比较数据框熊猫中的列? - how to compare columns in dataframe pandas? 如何比较 dask dataframe 的列? - How can I compare columns of a dask dataframe? 使用pandas,如何比较两个数据帧中2列之间的值并将它们推送到新的数据帧? - Using pandas, how can I compare the values between 2 columns from two dataframes and push them to a new dataframe? 如何使用 function 比较 pandas dataframe 中列中的值? - How do I compare values in columns in a pandas dataframe using a function? 如何字符串比较熊猫数据框中的两列? - how to string compare two columns in pandas dataframe? 如何一次比较四列pandas数据帧? - How to compare four columns of pandas dataframe at a time? 我如何比较 PySpark 中另一个 dataframe 的列 - How i can compare columns from another dataframe in PySpark 使用 pandas dataframe,如何将 dataframe 的列与数组中的条目进行比较? - Using pandas dataframe, how do I compare the columns of a dataframe to the entries in an array? (Python)如何将 2 列或更多列与 Pandas 进行比较? - (Python) How can I compare 2 or more columns with Pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM