Max value using idxmax

前端 未结 8 1443
情歌与酒
情歌与酒 2020-12-22 04:55

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

相关标签:
8条回答
  • 2020-12-22 05:14
    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 讨论(0)
  • 2020-12-22 05:14

    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 讨论(0)
  • 2020-12-22 05:15
    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 讨论(0)
  • 2020-12-22 05:17
    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 讨论(0)
  • 2020-12-22 05:21
    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 讨论(0)
  • 2020-12-22 05:22

    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()
    
    0 讨论(0)
提交回复
热议问题