Home > Net >  How to prune out certain results from pandas dataframes using and/or operators
How to prune out certain results from pandas dataframes using and/or operators

Time:11-19

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.

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

CodePudding user response:

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
  • Related