1

I am having an error: Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() I understood the context of this error but I am unable to fix this. Here ps is the list of either float or NnN that's it. In this case what it has caused it to be ambiguous to handle if statement? It gives an error on line 3, 5, 9 and lastly at sort statement.

               for item in candidates:
                    print("I am candidate loop \n",item)
                    ps=2*tmp3[0,i]/((diagonal.loc[x1])["pc"]+(diagonal.loc[item+1])["pc"])
                    print(ps)
                    if ps > 0:
                        if len(out)<k:
                            out.append([ps, item+1])
                        else:
                            if ps > min(out,key=lambda x:x[0])[0]):
                                out.remove(min(out,key=lambda x:x[0]))
                                out.append([ps,item+1])
                    else:
                        pass
                    i+=1
    
                out.sort()
                out.reverse()

I follwed this solution on this platform also tried this way just in case if all values are greater then zero The if loop continues but then it gives the same error on out.sort().

for item in candidates:
                    print("I am candidate loop \n",item)
                    ps=2*tmp3[0,i]/((diagonal.loc[x1])["pc"]+(diagonal.loc[item+1])["pc"])
                    print(ps)
                    if (ps > 0).all():
                        if len(out)<k:
                            out.append([ps, item+1])
                        else:
                            if (ps > min(out,key=lambda x:x[0])[0]).all():
                                out.remove(min(out,key=lambda x:x[0]))
                                out.append([ps,item+1])
                    else:
                        pass
                    i+=1
    
                out.sort()
                out.reverse()

Any kind of help would be appreciated!

Thanks in advance!

Sniper
  • 418
  • 3
  • 12
  • To find where the exception arises, you could step through the program in a debugger such as the PyCharm debugger. Then you can make a minimal test case to request help on if you're still stuck. Are you working with a list or a numpy array? – Jerry101 Apr 16 '21 at 03:29
  • Alright i will try to and I am working with numpy array. `ps` should only contain the result of `ps=2*tmp3[0,i]/((diagonal.loc[x1])["pc"]+(diagonal.loc[item+1])["pc"])` before `if` statement but it fails to calculate the `ps` for all candidate list. – Sniper Apr 16 '21 at 20:29

0 Answers0