首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ValueError:系列的真值不明确。使用a.empty、a.bool()、a.item()、a.any()或a.all()。在巢巢里,蟒蛇和熊猫

ValueError:系列的真值不明确。使用a.empty、a.bool()、a.item()、a.any()或a.all()。在巢巢里,蟒蛇和熊猫
EN

Stack Overflow用户
提问于 2020-11-25 13:59:13
回答 1查看 91关注 0票数 0

我有一个熊猫数据框架,"positions_deposits":

+------------+-----------------+---------------+----------------+--------+,参数,Amber_threshold,--+少点-少

如果很难读,在这里粘贴数据为纯文本(新到堆栈溢出,对不起:)

代码语言:javascript
复制
Parameter   Amber_threshold Red_threshold   Type_threshold  Values
Parameter1  10  5   Less    7
Parameter2  50  100 More    200
Parameter1  10  5   Less    60
Parameter2  50  100 More    10

我想创建另一列Calculated_status,它的值将是"Green“、"Amber”或"Red“,具体取决于以下逻辑:

代码语言:javascript
复制
def RAG_function (Amber_threshold, Red_threshold, Type_threshold, Values):
    if Type_threshold == 'Less':
        if Values <= Red_threshold:
            Status = 'Red'
        elif Values <=Amber_threshold:
            Status = 'Amber'
        else: Status = 'Green'
    if Type_threshold == 'More':
        if Values > Red_threshold:
            Status = 'Red'
        elif Values > Amber_threshold:
            Status = 'Amber'
        else: Status = 'Green'
    return Status

例如,在上述表格中,Calculated_status将分别为“琥珀”、“红色”、“绿色”和“绿色”。

将该功能调用为:

代码语言:javascript
复制
positions_deposits['Calculated_status'] = positions_deposits.apply(RAG_function(positions_deposits['Amber_threshold'], positions_deposits['Red_threshold'], positions_deposits['Type_threshold'], positions_deposits['Values']), axis =0)

我得到了以下错误:

代码语言:javascript
复制
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我做错了什么?顺便说一句,我和streamlit一起使用了整个程序。但是,这不应该重要。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-25 14:24:29

代码语言:javascript
复制
def function(x):

    all your conditions

    return status

dataframe["Calculated_status"] = dataframe.apply(function,axis=0)

这将完成这项工作,您现在所要做的就是定义您的函数。

Note

Python不识别elseif,它被称为elif

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65006187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档