threxx
threxx

Reputation: 1243

The truth value of a Series is ambiguous python dataframe

I am new to python pandas. I build a small function and now I always get the following error:

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

I know that this error was already discussed in other question, however I do not really get what I should do different and how the error occurred.

So this is my simple function:

def relativeWinner():
    if df['GoldSummer'] >0 & df['GoldWinter'] >0:
        df['diff'] = abs(df['GoldSummer'] - df['GoldWinter'])/(df['GoldSummer'] + df['GoldWinter'])
    return df['diff'].idxmax()

Can anyone tell me whats wrong here and how i would fix it?

Upvotes: 0

Views: 1441

Answers (1)

chad39
chad39

Reputation: 26

As for why this specific issue is occurring, see this post:

Difference between 'and' (boolean) vs. '&' (bitwise) in python. Why difference in behavior with lists vs numpy arrays?

Regarding your code, try this instead:

df['diff'] = [abs(tup[0] - tup[1]) / tup[0] if (tup[0] > 0) and (tup[1] > 0) else 'NaN' for tup in zip(df['GoldSummer'], df['GoldWinter'])]

Upvotes: 1

Related Questions