Home > Software design >  How to solve this ValueError in python?
How to solve this ValueError in python?

Time:11-23

for x in (df.index):
        if(df.loc[x]['hour']==9):
            print(True)

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

CodePudding user response:

This issue here is that the index is not unique, this is causing df.loc[x]['hour'] to return a Series not a single value it would be better to simply loop over the values you are checking

for x in (df.hour):
    if(x==9):
        print(True)

Output

True
True
True
  • Related