简体   繁体   English

Pandas 最大值索引

[英]Pandas max value index

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 混合了屏幕名称、推文、fav 等。我想找到“favcount”的最大值(我已经完成了)并返回那个“tweet”的屏幕名称

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?我似乎在这方面找不到任何东西,任何人都可以帮助指导我正确的方向吗?

Use argmax() idxmax() to get the index of the max value.使用argmax() idxmax()获取最大值的索引。 Then you can use loc然后你可以使用loc

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

Edit: argmax() is now deprecated, switching for idxmax()编辑: argmax()现在已弃用,切换为idxmax()

I think you need idxmax - get index of max value of favcount and then select value in column sn by loc :我认为您需要idxmax - 获取idxmax最大值的favcount ,然后通过loc在列sn选择值:

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

By using same df as above,通过使用与上述相同的 df,

# 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM