Python 熊猫最大值指数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39964558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas max value index
提问by mGarsteck
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 name of that 'tweet'
我有一个混合了屏幕名称、推文、收藏夹等的 Pandas DataFrame。我想找到“favcount”(我已经完成)的最大值,并返回该“推文”的屏幕名称
df = pd.DataFrame()
df['timestamp'] = timestamp
df['sn'] = sn
df['text'] = text
df['favcount'] = fav_count
print df
print '------'
print df['favcount'].max()
I cant seem to find anything on this, can anyone help guide me in the right direction?
我似乎无法找到任何关于此的信息,任何人都可以帮助指导我朝着正确的方向前进吗?
回答by Steven G
Use argmax()
idxmax()
to get the index of the max value. Then you can use loc
使用 来获取最大价值的指标。然后你可以使用argmax()
idxmax()
loc
df.loc[df['favcount'].idxmax(), 'sn']
Edit:argmax()
is now deprecated, switching foridxmax()
编辑:argmax()
现在已弃用,切换为idxmax()
回答by jezrael
I think you need idxmax
- get index of max value of favcount
and then select value in column sn
by loc
:
我认为您需要idxmax
- 获取最大值的索引,favcount
然后sn
通过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