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 to prune out certain results from pandas dataframes using and/or operators

I have the following dataframe named state:

     SSSLifestress  SSSHealthstress  SSSFinancialstress  SSSSocialstress
0               61               80                  78               46
1               62               85                  19               75
2               63               57                  62               21
3               64               11                  90               26
4               65               31                  77               48

and I want to prune out a high scale and low scale where lifestress >= 63 AND either one of the three is true where (healthStress >= 63 OR ssFinance >= 63 OR socialstress >= 63)

So lifestress must be >= 63 and one of the three others must be >= 63 as well as <= 33 for the low scale, same as above.

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

I have the following code here

high_scale1 = ( state[state['SSSLifestress']>=63].reset_index(drop=True) & (state[state['SSSHealthstress']>=63] | state[state['SSSFinancialstress']>=63] | state[state['SSSSocialstress']>=63])).reset_index(drop=True) 
low_scale1 =  (state[state['SSSLifestress']<=33].reset_index(drop=True) & (state[state['SSSHealthstress']<=33] | state[state['SSSFinancialstress']<=33] | state[state['SSSSocialstress']<=33])).reset_index(drop=True)    

however I get the error of:

TypeError: unsupported operand type(s) for |: 'float' and 'bool'

I’m looking for the following output for the high scale:

     SSSLifestress  SSSHealthstress  SSSFinancialstress  SSSSocialstress
0               64               11                  90               26
1               65               31                  77               48

>Solution :

You don’t need to create multiple dataframes and reset their indexes. Just put certain conditions in .loc function. For high scale it would be:

high_scale1 = state.loc[(state['SSSLifestress']>=63) &
                        ((state['SSSHealthstress']>=63) |
                        (state['SSSFinancialstress']>=63) |
                        (state['SSSSocialstress']>=63)), 
                        :].reset_index(drop=True)

Output:

    SSSLifestress   SSSHealthstress  SSSFinancialstress  SSSSocialstress
0              64                11                  90               26
1              65                31                  77               48
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