系列。最大值和 idxmax

我试图获得最大值以及系列对象的相应索引。

s = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])

s.max() 将返回最大值,而 s.idxmax() 将返回最大值的索引。有没有一种方法可以让我们获取值及其相应的索引?

谢谢你。


撒科打诨
浏览 238回答 2
2回答

动漫人物

自定义函数呢?就像是import numpy as npimport pandas as pds = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])def Max_Argmax(series):  # takes as input your series   values = series.values  # store numeric values   indexes = series.index  # store indexes   Argmax = np.argmax(values)  # save index of max   return values[Argmax], indexes[Argmax] # return max and corresponding index(max, index) = Max_Argmax(s)我在我的电脑上运行它,我得到:>>> sa   -1.854440b    0.302282c   -0.630175d   -1.012799e    0.239437dtype: float64>>> max0.3022819091746019>>> index'b'希望能帮助到你!

湖上湖

正如乔恩·克莱门茨所说:In [3]: s = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])In [4]: x, y = s.agg(['max', 'idxmax'])In [5]: xOut[5]: 1.6339096862287581In [6]: yOut[6]: 'b'In [7]: sOut[7]: a&nbsp; &nbsp; 1.245039&nbsp; &nbsp; &nbsp; &nbsp; b&nbsp; &nbsp; 1.633910&nbsp; &nbsp; &nbsp; &nbsp; c&nbsp; &nbsp; 0.619384&nbsp; &nbsp; &nbsp; &nbsp; d&nbsp; &nbsp; 0.369604&nbsp; &nbsp; &nbsp; &nbsp; e&nbsp; &nbsp; 1.009942&nbsp; &nbsp; &nbsp; &nbsp; dtype: float64响应请求元组:def max_and_index(series):&nbsp; &nbsp; """Return a tuple of (max, idxmax) from a pandas.Series"""&nbsp; &nbsp; x, y = series.agg(['max', 'idxmax'])&nbsp; &nbsp; return x, yt = max_and_idxmax(s)print(t)(1.6339096862287581, 'b')print(type(t))<class 'tuple'>更小:def max_and_idxmax(series):&nbsp; &nbsp; """Return a tuple of (max, idxmax) from a pandas.Series"""&nbsp; &nbsp; return series.max(), series.idxmax()如果需要速度,请使用上面的numpy方法import pandas as pdimport numpy as npdef max_and_index(series):&nbsp; &nbsp; x, y = series.agg(['max', 'idxmax'])&nbsp; &nbsp; return x, ydef max_and_idxmax(series):&nbsp; &nbsp; return series.max(), series.idxmax()def np_max_and_argmax(series):&nbsp; &nbsp; return np.max(series.values), np.argmax(series.values)def Max_Argmax(series):&nbsp; &nbsp;v = series.values&nbsp; &nbsp;i = series.index&nbsp; &nbsp;arg = np.argmax(v)&nbsp; &nbsp;return v[arg], i[arg]a = []for i in range(2,9,1):&nbsp; &nbsp; a.append(pd.Series(np.random.randint(0, 100, size=10**i)))&nbsp; &nbsp; print('{}\t{:>11,}'.format(i-2, 10**i))# 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 100# 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1,000# 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;10,000# 3&nbsp; &nbsp; &nbsp; &nbsp; 100,000# 4&nbsp; &nbsp; &nbsp; 1,000,000# 5&nbsp; &nbsp; &nbsp;10,000,000# 6&nbsp; &nbsp; 100,000,000idx = 5%%timeit -n 2 -r 10max_and_index(a[idx])# 144 ms ± 5.45 ms per loop (mean ± std. dev. of 10 runs, 2 loops each)%%timeit -n 2 -r 10max_and_idxmax(a[idx])# 143 ms ± 5.14 ms per loop (mean ± std. dev. of 10 runs, 2 loops each)%%timeit -n 2 -r 10Max_Argmax(a[idx])# 9.89 ms ± 1.13 ms per loop (mean ± std. dev. of 10 runs, 2 loops each)%%timeit -n 2 -r 10np_max_and_argmax(a[idx])# 24.5 ms ± 1.74 ms per loop (mean ± std. dev. of 10 runs, 2 loops each)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python