[英]How to get column name that has max value in a specific row?
我需要獲取具有特定行最大值的列名。
a b c
0 2 2 1
1 1 5 5
2 1 1 5
3 1 1 3
4 5 4 1
5 5 2 5
我希望在 index=2 中搜索時得到 c 列,在 index=4 中搜索時得到 a 列。
我試過這個解決方案,但它不起作用。 在我的真實 df 中,當我使用 df.info() 檢查時,它顯示 Dtype 是對象而不是數字。
你寫道dtype是object ,但另一方面,你的數據樣本只包含整數,我認為它是一個有代表性的樣本。
所以第一步是將dtype更改為int :
df = df.astype(int)
然后使用以下函數:
def maxColName(df, ind):
return df.loc[ind].idxmax()
或者您可以刪除df參數並將此名稱用作全局變量:
def maxColName(ind):
return df.loc[ind].idxmax()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.