1

I have a pandas dataframe called month_values with values such as the following:

Area Thrpt_A Thrpt_B Thrpt_C Thrpt_D
Galle 9581.539079 6790.664649 8340.489900 5940.886763
Colombo 5948.006563 4245.619220 7084.931812 3915.933362

The datatypes are

Area             object
Thrpt_A          float64
Thrpt_B          float64
Thrpt_C          float64
Thrpt_D          float64

I've defined a function with nested if-else to be applied to this dataframe as follows:

def mark_func(df):
    if (df['Area'] == "Colombo"):
        if (df['Thrpt_A'] >= 8000):
            return 'A'
        elif (df['Thrpt_A'] < (8000)):
            if (df['Thrpt_A'] >= df['Thrpt_B']) and (['Thrpt_A'] >= df['Thrpt_C']) and (['Thrpt_A'] >= df['Thrpt_D']):
                return 'A'
            elif (df['Thrpt_B'] >= df['Thrpt_C']) and (df['Thrpt_B'] >= df['Thrpt_D']):
                return 'B'
            elif (df['Thrpt_C'] >= df['Thrpt_B']) and (df['Thrpt_C'] >= df['Thrpt_D']):
                return 'C'
            elif (df['Thrpt_D'] >= df['Thrpt_B']) and (df['Thrpt_D'] >= df['Thrpt_C']):
                return 'D'
    else:
        if (df['Thrpt_A'] >= 4000):
            return 'A'
        elif (df['Thrpt_A'] < (4000)):
            if (df['Thrpt_A'] >= df['Thrpt_B']) and (['Thrpt_A'] >= df['Thrpt_C']) and (['Thrpt_A'] >= df['Thrpt_D']):
                return 'A'
            elif (df['Thrpt_B'] >= df['Thrpt_C']) and (df['Thrpt_B'] >= df['Thrpt_D']):
                return 'B'
            elif (df['Thrpt_C'] >= df['Thrpt_B']) and (df['Thrpt_C'] >= df['Thrpt_D']):
                return 'C'
            elif (df['Thrpt_D'] >= df['Thrpt_B']) and (df['Thrpt_D'] >= df['Thrpt_C']):
                return 'D'

The output I'm expecting is as follows:

Area Thrpt_A Thrpt_B Thrpt_C Thrpt_D Mark
Galle 9581.539079 6790.664649 8340.489900 5940.886763 A
Colombo 5948.006563 4245.619220 7084.931812 3915.933362 C

However, when I apply the function using the following line of code:

month_values['Mark'] = month_values.apply(mark_func, axis=1)

I get the following error:

TypeError: ("'>=' not supported between instances of 'list' and 'float'", 'occurred at index 279')

I'm not exactly sure what I'm doing wrong here. Would be grateful for any suggestion to resolve the issue.

1 Answer 1

1

This part of the code:

... and (['Thrpt_A'] >= df['Thrpt_C']) and (['Thrpt_A'] >= df['Thrpt_D']):

should be:

... and (df['Thrpt_A'] >= df['Thrpt_C']) and (df['Thrpt_A'] >= df['Thrpt_D']):

Note the df

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.