尽管使用了正确的运算符,但条件语句存在问题

时间:2019-02-20 01:28:19

标签: python pandas numpy boolean conditional-statements

我写了这个脚本来创建一个特定的变量,该变量根据报告的数量采用不同的值。报告计数是一个整数列。

no_audit = df_bei_index['Count of Report'] == 0 
few_audit = df_bei_index['Count of Report'] > 0 & df_bei_index['Count of Report'] < 30

col_list = ['Policy Index (ELEVATE)_score', 'Transparency Score']

for col in col_list:
        df_bei_index[col+'_corrected'] = np.where(m1, df_bei_index['PDI_Average'], np.where( 
                                                  m2, df_bei_index[col]*0.05 + df_bei_index['PDI_Average']*0.95
                                                  , df_bei_index[col])) 

但是,当我运行它时,出现以下错误:

> --------------------------------------------------------------------------- ValueError                                Traceback (most recent call
> last) <ipython-input-42-6fe73dad7759> in <module>()
>      41 no_audit = df_bei_index['Count of Report'] == 0
>      42 df_bei_index
> ---> 43 few_audit = df_bei_index[(df_bei_index['Count of Report'] > 0 & df_bei_index['Count of Report'] < 30)]
>      44 
>      45 
> 
> ~\Anaconda3\lib\site-packages\pandas\core\generic.py in
> __nonzero__(self)    1574         raise ValueError("The truth value of a {0} is ambiguous. "    1575                          "Use a.empty,
> a.bool(), a.item(), a.any() or a.all()."
> -> 1576                          .format(self.__class__.__name__))    1577     1578     __bool__ = __nonzero__
> 
> ValueError: The truth value of a Series is ambiguous. Use a.empty,
> a.bool(), a.item(), a.any() or a.all().

我环顾四周,当您使用'and'或'or'而不是'&'或'|'时出现此错误,但是显然这不是我的情况。

脚本有什么问题?

1 个答案:

答案 0 :(得分:2)

通过添加m2来更改()

m1= df_bei_index['Count of Report'] == 0 
m2= (df_bei_index['Count of Report'] > 0) & (df_bei_index['Count of Report'] < 30)