Pandas Dataframe:获取最大元素的索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29972894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:17:29  来源:igfitidea点击:

Pandas Dataframe: get index of max element

pythonindexingpandasdataframe

提问by JNevens

I am looking for a way to get both the index and the column of the maximum element in a Pandas DataFrame. Thus far, this is my code:

我正在寻找一种方法来获取 Pandas DataFrame 中最大元素的索引和列。到目前为止,这是我的代码:

idx = range(0, 50, 5)
col = range(0, 50, 5)
scores = pd.DataFrame(np.zeros((len(idx), len(col))), index=idx, columns=col, dtype=float)
scores.loc[11, 16] = 5 #Assign a random element

This gives me the following DataFrame:

这给了我以下数据帧:

  | 1   6   11  16  21  26  31  36  41  46
------------------------------------------
1 | 0   0   0   0   0   0   0   0   0   0
6 | 0   0   0   0   0   0   0   0   0   0
11| 0   0   0   5   0   0   0   0   0   0
16| 0   0   0   0   0   0   0   0   0   0
21| 0   0   0   0   0   0   0   0   0   0
26| 0   0   0   0   0   0   0   0   0   0
31| 0   0   0   0   0   0   0   0   0   0
36| 0   0   0   0   0   0   0   0   0   0
41| 0   0   0   0   0   0   0   0   0   0
46| 0   0   0   0   0   0   0   0   0   0

After that, I use the unstackmethod:

之后,我使用以下unstack方法:

unstacked = scores.unstack().copy()
unstacked.sort(ascending=False)

This gives me:

这给了我:

16  11    5
46  46    0
16  31    0
11  31    0
    36    0
...

How can I get the index and column of the maximum value? I would like to get something along the lines of an array or tuple containing (16, 11).

如何获取最大值的索引和列?我想得到一些类似于包含(16, 11).

回答by fixxxer

You are looking for idxmax:

您正在寻找idxmax

In [1332]: x
Out[1332]: 
   1  6  11  16  21  26  31  36  41  46
0  0  0   0   0   0   0   0   0   0   0
1  0  0   0   0   0   0   0   0   0   0
2  0  0   5   0   0   0   0   0   0   0
3  0  0   0   0   0   0   0   0   0   0
4  0  0   0   0   0   0   0   0   0   0
5  0  0   0   0   0   0   0   0   0   0
6  0  0   0   0   0   0   0   0   0   0
7  0  0   0   0   0   0   0   0   0   0
8  0  0   0   0   0   0   0   0   0   0
9  0  0   0   0   0   0   0   0   0   0

Row of the max value:

最大值的行:

In [1337]: max(x.idxmax())
Out[1337]: 2

Column of the max value (too many maxs):

最大值的列(太多maxs):

In [1359]: x.max()[x.max() == x.max(axis=1).max()].index
Out[1359]: Index([u'11'], dtype='object')

回答by mik

x.max()[x.max() == x.max(axis=1).max()].index 

This works to get the column but max(x.idxmax())only returns the numerical maximum of the indices themselves, not the index of the maximum value in the table (just got lucky in this example because everything else is 0's). An alternative is:

这适用于获取列,但max(x.idxmax())只返回索引本身的数值最大值,而不是表中最大值的索引(在本例中很幸运,因为其他一切都是 0)。另一种选择是:

s = x.max()[x.max() == x.max(index=1).max()].index
s = str(s[0])
max_index = x.idxmax()[s]