我有一个熊猫数据框架,"positions_deposits":
+------------+-----------------+---------------+----------------+--------+,参数,Amber_threshold,--+少点-少
如果很难读,在这里粘贴数据为纯文本(新到堆栈溢出,对不起:)
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“,具体取决于以下逻辑:
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将分别为“琥珀”、“红色”、“绿色”和“绿色”。
将该功能调用为:
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)我得到了以下错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().我做错了什么?顺便说一句,我和streamlit一起使用了整个程序。但是,这不应该重要。
发布于 2020-11-25 14:24:29
def function(x):
all your conditions
return status
dataframe["Calculated_status"] = dataframe.apply(function,axis=0)这将完成这项工作,您现在所要做的就是定义您的函数。
Note
Python不识别elseif,它被称为elif。
https://stackoverflow.com/questions/65006187
复制相似问题