湖上湖
正如乔恩·克莱门茨所说: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 1.245039 b 1.633910 c 0.619384 d 0.369604 e 1.009942 dtype: float64响应请求元组:def max_and_index(series): """Return a tuple of (max, idxmax) from a pandas.Series""" x, y = series.agg(['max', 'idxmax']) return x, yt = max_and_idxmax(s)print(t)(1.6339096862287581, 'b')print(type(t))<class 'tuple'>更小:def max_and_idxmax(series): """Return a tuple of (max, idxmax) from a pandas.Series""" return series.max(), series.idxmax()如果需要速度,请使用上面的numpy方法import pandas as pdimport numpy as npdef max_and_index(series): x, y = series.agg(['max', 'idxmax']) return x, ydef max_and_idxmax(series): return series.max(), series.idxmax()def np_max_and_argmax(series): return np.max(series.values), np.argmax(series.values)def Max_Argmax(series): v = series.values i = series.index arg = np.argmax(v) return v[arg], i[arg]a = []for i in range(2,9,1): a.append(pd.Series(np.random.randint(0, 100, size=10**i))) print('{}\t{:>11,}'.format(i-2, 10**i))# 0 100# 1 1,000# 2 10,000# 3 100,000# 4 1,000,000# 5 10,000,000# 6 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)