Pandas max value index

后端 未结 3 1546
耶瑟儿~
耶瑟儿~ 2020-11-27 18:17

I have a Pandas DataFrame with a mix of screen names, tweets, fav\'s etc. I want find the max value of \'favcount\' (which i have already done) and also return the screen n

相关标签:
3条回答
  • 2020-11-27 18:50

    I think you need idxmax - get index of max value of favcount and then select value in column sn by loc:

    df = pd.DataFrame({'favcount':[1,2,3], 'sn':['a','b','c']})
    
    print (df)
       favcount sn
    0         1  a
    1         2  b
    2         3  c
    
    print (df.favcount.idxmax())
    2
    
    print (df.loc[df.favcount.idxmax()])
    favcount    3
    sn          c
    Name: 2, dtype: object
    
    print (df.loc[df.favcount.idxmax(), 'sn'])
    c
    
    0 讨论(0)
  • 2020-11-27 19:08

    Use argmax() idxmax() to get the index of the max value. Then you can use loc

    df.loc[df['favcount'].idxmax(), 'sn']
    

    Edit: argmax() is now deprecated, switching for idxmax()

    0 讨论(0)
  • 2020-11-27 19:12

    By using same df as above,

    # python code
    

    df = pd.DataFrame({'favcount':[1,2,3], 'sn':['a','b','c']})

    print (df) favcount sn 0 1 a 1 2 b 2 3 c

    ## You can use max() print(df[df.favcount.max() == df['favcount']])

    favcount sn 2 3 c

    ## If you need specific column you can select it print(df[df.favcount.max() == df['favcount']].sn)

    2 c

    0 讨论(0)
提交回复
热议问题