簡體   English   中英

Pandas 最大值索引

[英]Pandas max value index

我有一個 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()

我似乎在這方面找不到任何東西,任何人都可以幫助指導我正確的方向嗎?

使用argmax() idxmax()獲取最大值的索引。 然后你可以使用loc

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

編輯: argmax()現在已棄用,切換為idxmax()

我認為您需要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

通過使用與上述相同的 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