![](/img/trans.png)
[英]Pandas Dataframe: groupby id to find max column value and return corresponding value of another column
[英]How do I take max value of a pandas dataframe column and find the corresponding value in another column?
我想獲取熊貓數據框列的最大值並在另一列中找到相應的值? 假設最大值中沒有重復項,因此您始終只返回一個值。 例如,
GPA ID
2.3 Tina1
3.4 Bob1
3.6 Lia1
2.9 Tina2
4.0 Blake1
4.5 Conor2
這里的最大 GPA 是 4.5,但我想返回最大 GPA 對應的 id,所以我會返回 Conor2。 我不確定如何執行此操作,因此非常感謝您的幫助:) 謝謝!
您可以使用
df.loc[df.GPA == df.GPA.max(), 'ID']
你得到
5 Conor2
如果您不想要索引而只想要值,請嘗試
df.loc[df.GPA == df.GPA.max(), 'ID'].values[0]
你得到
'Conor2'
讓我們使用idxmax
:
df.set_index('ID').idxmax()
輸出:
GPA Conor2
dtype: object
具有單個值Conor2
系列。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.