Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I compare columns of a pandas dataframe?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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")
   ..

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading