Home > front end >  The truth value of a Series is ambiguous. I have tried using the available answers but nothing worke
The truth value of a Series is ambiguous. I have tried using the available answers but nothing worke

Time:05-05

I'm getting error

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

I have tried replacing and with '&' but still it didn't work.

roc_table = pd.DataFrame(columns = ['score','TP', 'FP','TN','FN'])

TP=0
FP=0
TN=0
FN=0

df_roc = pd.DataFrame([["score", "status"], [100, "accept"], [-80, "reject"]])

for score in range(-100,-80,5):
    for row in df_roc.iterrows():
        if (df_roc['score'] >= score) & (df_roc['status'] == 'reject'):
            TP=TP 1
        elif (df_roc['score'] >= score) & (df_roc['status'] == 'accept'):
            FP=FP 1
        elif (df_roc['score'] < score) & (df_roc['status'] == 'accept'):
            TN=TN 1
        elif (df_roc['score'] < score) & (df_roc['status'] == 'reject'):
            FN=FN 1
            
    dict = {'score':score, 'TP': TP, 'FP': FP, 'TN': TN,'FN':FN}    
    roc_table = roc_table.append(dict, ignore_index = True)

sample of df_roc:

score status
100 accept
-80 reject

CodePudding user response:

df_roc['score'] >= score

The error is telling you that this comparison makes no sense.

df_roc['score'] is a column containing many values. Some may be less than score, and others may be greater.

Since this code is inside a for row in df_roc.iterrows() loop, I think you intended to compare just the score from the current row, but that's not what you actually did.

CodePudding user response:

I changed the code to this and it worked. Thanks

(row['score'] >= score) & (row['status'] == 'reject')
  • Related