0
votes

I am trying to calculate the biggest difference between summer gold medal counts and winter gold medal counts relative to their total gold medal count. The problem is that I need to consider only countries that have won at least 1 gold medal in both summer and winter.

Gold: Count of summer gold medals

Gold.1: Count of winter gold medals

Gold.2: Total Gold

This a sample of my data:

            Gold    Gold.1  Gold.2  ID  diff gold %
Afghanistan 0       0       0       AFG NaN
Algeria     5       0       5       ALG 1.000000
Argentina   18      0       18      ARG 1.000000
Armenia     1       0       1       ARM 1.000000
Australasia 3       0       3       ANZ 1.000000
Australia   139     5       144     AUS 0.930556
Austria     18      59      77      AUT 0.532468
Azerbaijan  6       0       6       AZE 1.000000
Bahamas     5       0       5       BAH 1.000000
Bahrain     0       0       0       BRN NaN
Barbados    0       0       0       BAR NaN
Belarus     12      6       18      BLR 0.333333

This is the code that I have but it is giving the wrong answer:

def answer():
    Gold_Y = df2[(df2['Gold'] > 1) | (df2['Gold.1'] > 1)]
    df2['difference'] = (df2['Gold']-df2['Gold.1']).abs()/df2['Gold.2']
    return df2['diff gold %'].idxmax()
   
answer()  
8
If there is anything that is not clear in my question I will appreciate if you guys can comment instead of downgrading my question. I am new to stack flow and I don't understand the whole formatting deal!!!nanny

8 Answers

1
votes

Try this code after subbing in the correct (your) function and variable names. I'm new to Python, but I think the issue was that you had to use the same variable in Line 4 (df1['difference']), and just add the method (.idxmax()) to the end. I don't think you need the first line of code for the function, either, as you don't use the local variable (Gold_Y). FYI - I don't think we're working with the same dataset.

def answer_three():
    df1['difference'] = (df1['Gold']-df1['Gold.1']).abs()/df1['Gold.2']
    return df1['difference'].idxmax()

answer_three()
1
votes
def answer_three():
     atleast_one_gold = df[(df['Gold']>1) & (df['Gold.1']> 1)]
     return ((atleast_one_gold['Gold'] - atleast_one_gold['Gold.1'])/atleast_one_gold['Gold.2']).idxmax()

answer_three()
0
votes
def answer_three():
    _df = df[(df['Gold'] > 0) & (df['Gold.1'] > 0)]
    return ((_df['Gold'] - _df['Gold.1']) / _df['Gold.2']).argmax() answer_three()
0
votes

This looks like a question from the programming assignment of courser course - "Introduction to Data Science in Python"

Having said that if you are not cheating "maybe" the bug is here:

Gold_Y = df2[(df2['Gold'] > 1) | (df2['Gold.1'] > 1)]

You should use the & operator. The | operator means you have countries that have won Gold in either the Summer or Winter olympics.

You should not get a NaN in your diff gold.

0
votes
def answer_three():
    diff=df['Gold']-df['Gold.1']
    relativegold = diff.abs()/df['Gold.2']
    df['relativegold']=relativegold
    x = df[(df['Gold.1']>0) &(df['Gold']>0) ]
    return x['relativegold'].idxmax(axis=0)

answer_three()
0
votes

I an pretty new to python or programming as a whole. So my solution would be the most novice ever! I love to create variables; so you'll see a lot in the solution.

    def answer_three:
      a = df.loc[df['Gold'] > 0,'Gold']
           #Boolean masking that only prints the value of Gold that matches the condition as stated in the question; in this case countries who had at least one Gold medal in the summer seasons olympics.

      b = df.loc[df['Gold.1'] > 0, 'Gold.1']
           #Same comment as above but 'Gold.1' is Gold medals in the winter seasons 

      dif = abs(a-b)

           #returns the abs value of the difference between a and b.

      dif.dropna()

           #drops all 'Nan' values in the column.

      tots = a + b

           #i only realised that this step wasn't essential because the data frame had already summed it up in the column 'Gold.2'

      tots.dropna()

      result = dif.dropna()/tots.dropna()

      returns result.idxmax

           # returns the index value of the max result
0
votes
def answer_two():
    df2=pd.Series.max(df['Gold']-df['Gold.1'])
    df2=df[df['Gold']-df['Gold.1']==df2]

    return df2.index[0]
answer_two()
0
votes
def answer_three():
    return ((df[(df['Gold']>0) & (df['Gold.1']>0 )]['Gold'] - df[(df['Gold']>0) & (df['Gold.1']>0 )]['Gold.1'])/df[(df['Gold']>0) & (df['Gold.1']>0 )]['Gold.2']).argmax()