0
def answer_three():
    if(df['Gold.2']>=1):
      return df[df['diff']/df['Gold.2'].max()].index[0]
answer_three()

By writing this code I got an error like this

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

where diff is:

df['diff'] = abs(df['Gold'] - df['Gold.1'])
realr
  • 3,652
  • 6
  • 23
  • 34
  • Does this answer your question? [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – filbranden Jan 19 '20 at 19:19

1 Answers1

1

The problem is that you are trying to compare a Serie (df['Gold.2']) with a number, basically what Python is seeing is something similar to this: [0 4 6 7 4 2 0 4] >= 1 and returning [False True True True True True False True].

So when you try to check if [False True True True True True False True] python doesn't know what to do and ask you to use one of any, empty, all, etc methods that make this list to flatten into one.

If you can explain what is exactly what you want I could help you to also fix the code.

sergiomahi
  • 964
  • 2
  • 8
  • 21
  • there are actually two columns and I have to find Which country had the biggest difference between their summer and winter gold medal counts? from a github profile I got an ans like this def answer_three(): sub_df = df[(df['Gold'] > 0) & (df['Gold.1'] > 0)].copy() sub_df['rel'] = sub_df['diff'] / sub_df['Gold.2'] return sub_df[sub_df['rel'] == max(sub_df['rel'])].index[0] answer_three() – Ritik Sharma Jan 28 '20 at 15:54