Slicing with a logical (boolean) expression a Pandas Dataframe

后端 未结 2 741
既然无缘
既然无缘 2021-01-20 15:59

I am getting an exception as I try to slice with a logical expression my Pandas dataframe.

My data have the following form:

df
    GDP_norm    SP500_         


        
2条回答
  •  逝去的感伤
    2021-01-20 17:02

    You are suffering from the effects of chained comparisons. What's happening is the expression df['GDP_norm'] >=3.5 & df['GDP_norm'] <= 4.5 is evaluated as something like:

    df['GDP_norm'] >= (3.5 & df['GDP_norm']) <= 4.5
    

    Of course, this fails since float cannot be compared with bool, as described in your error message. Instead, use parentheses to isolate each Boolean mask and assign to variables:

    m1 = (df['GDP_norm'] >= 3.5) & (df['GDP_norm'] <= 4.5)
    m2 = df['SP500_Index_deflated_norm'] > 3
    
    m3 = (df['GDP_norm'] >= 4.0) & (df['GDP_norm'] <= 5.0)
    m4 = df['SP500_Index_deflated_norm'] < 3.5
    
    res = df[(m1 & m2) | (m3 & m4)]
    

提交回复
热议问题