尽管使用了右操作符[条件语句]的问题[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

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

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().

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

脚本有什么问题?

python pandas numpy boolean condition
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.