0
votes

I have a pandas data frame, "positions_deposits":

+------------+-----------------+---------------+----------------+--------+ | 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 | +------------+-----------------+---------------+----------------+--------+

If it is difficult to read, pasting data as plain text here (new to stackoverflow, sorry :(

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

I want to create another column, Calculated_status whose values will be "Green", "Amber" or "Red" depending on the following logic:

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

E.g., in the above-mentioned table, the Calculated_status will be 'Amber', 'Red', 'Green' and 'Green' respectively.

Calling the function as:

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)

I am getting the following error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

What am I doing wrong? Btw, I am using the entire thing along with streamlit. But, it should not matter.

1

1 Answers

0
votes
def function(x):

    all your conditions

    return status

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

This will do the job, all you have to do now is define your function.

Note

Python doesn't recognize elseif it is called elif.