使用逻辑(布尔)表达式对 Pandas Dataframe 进行切片

use*_*077 5 python boolean-expression slice logical-operators pandas

当我尝试使用逻辑表达式对 Pandas 数据框进行切片时,出现异常。

我的数据具有以下形式:

df
    GDP_norm    SP500_Index_deflated_norm
Year        
1980    2.121190    0.769400
1981    2.176224    0.843933
1982    2.134638    0.700833
1983    2.233525    0.829402
1984    2.395658    0.923654
1985    2.497204    0.922986
1986    2.584896    1.09770

df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 38 entries, 1980 to 2017
Data columns (total 2 columns):
GDP_norm                     38 non-null float64
SP500_Index_deflated_norm    38 non-null float64
dtypes: float64(2)
memory usage: 912.0 bytes
Run Code Online (Sandbox Code Playgroud)

命令如下:

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

   (df['GDP_norm'] >= 4.0 & df['GDP_norm'] <= 5.0) & (df['SP500_Index_deflated_norm'] < 3.5))]
Run Code Online (Sandbox Code Playgroud)

错误信息如下:

TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]
Run Code Online (Sandbox Code Playgroud)

jez*_*ael 7

我建议单独创建布尔掩码以获得更好的可读性并且更容易处理错误。

\n\n

这里缺少()m1代码m2,问题在于运算符优先级:

\n\n

文档- 6.16。运算符优先级其中 see&具有更高的优先级,如下所示>=

\n\n
Operator                                Description\n\nlambda                                  Lambda expression\nif \xe2\x80\x93 else                               Conditional expression\nor                                      Boolean OR\nand                                     Boolean AND\nnot x                                   Boolean NOT\nin, not in, is, is not,                 Comparisons, including membership tests    \n<, <=, >, >=, !=, ==                    and identity tests\n|                                       Bitwise OR\n^                                       Bitwise XOR\n&                                       Bitwise AND\n\n(expressions...), [expressions...],     Binding or tuple display, list display,       \n{key: value...}, {expressions...}       dictionary display, set display\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n
m1 = (df[\'GDP_norm\'] >=3.5) & (df[\'GDP_norm\'] <= 4.5)\nm2 = (df[\'GDP_norm\'] >= 4.0) & (df[\'GDP_norm\'] <= 5.0)\n\nm3 = m1 & (df[\'SP500_Index_deflated_norm\'] > 3)\nm4 = m2 & (df[\'SP500_Index_deflated_norm\'] < 3.5)\n\ndf[m3 | m4]\n
Run Code Online (Sandbox Code Playgroud)\n