簡體   English   中英

如何在 Pandas 數據框中找到最大值的行和列的索引?

[英]How do I find the indices of the row and the column for the maximum value in a Pandas dataframe?

我有一個大 Pandas 數據框,我想找出最大值所在的列和行(在整個數據框中)。 不幸的是, df.idxmax()只返回每行/列的最高值的索引,而不是整個數據幀的索引。 有沒有辦法做到這一點?

假設您的數據幀中的所有數據都是數字,您可以通過將數據幀轉換為 numpy 數組來輕松完成:

import numpy as np
df = np.array(df) # Assuming df is your dataframe name
print(np.unravel_index(df.argmax(), df.shape))

采用:

# calculates the maximum of each row
# returns the index of the max of max 
df.apply(lambda x: max(x.values), axis=1).idxmax()

使用 numpy,您可以嘗試:

import numpy as np
>>> np.unravel_index(np.argmax(df.values), df.shape)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM