Slicing with a logical (boolean) expression a Pandas Dataframe

て烟熏妆下的殇ゞ 提交于 2019-12-01 21:19:36

I suggest create boolean masks separately for better readibility and also easier error handling.

Here are missing () in m1 and m2 code, problem is in operator precedence:

docs - 6.16. Operator precedence where see & have higher priority as >=:

Operator                                Description

lambda                                  Lambda expression
if – else                               Conditional expression
or                                      Boolean OR
and                                     Boolean AND
not x                                   Boolean NOT
in, not in, is, is not,                 Comparisons, including membership tests    
<, <=, >, >=, !=, ==                    and identity tests
|                                       Bitwise OR
^                                       Bitwise XOR
&                                       Bitwise AND

(expressions...), [expressions...],     Binding or tuple display, list display,       
{key: value...}, {expressions...}       dictionary display, set display

m1 = (df['GDP_norm'] >=3.5) & (df['GDP_norm'] <= 4.5)
m2 = (df['GDP_norm'] >= 4.0) & (df['GDP_norm'] <= 5.0)

m3 = m1 & (df['SP500_Index_deflated_norm'] > 3)
m4 = m2 & (df['SP500_Index_deflated_norm'] < 3.5)

df[m3 | m4]

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)]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!