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.