系列。 max和idxmax

时间:2018-10-30 08:07:58

标签: python python-3.x pandas

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

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

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

谢谢。

2 个答案:

答案 0 :(得分:2)

自定义函数呢?像

import numpy as np
import pandas as pd

s = 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)

我在PC上运行它,然后得到:

>>> s
a   -1.854440
b    0.302282
c   -0.630175
d   -1.012799
e    0.239437
dtype: float64

>>> max
0.3022819091746019

>>> index
'b'

希望有帮助!

答案 1 :(得分:2)

正如乔恩·克莱门茨(Jon Clements)所述:

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]: x
Out[5]: 1.6339096862287581
In [6]: y
Out[6]: 'b'
In [7]: s
Out[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, y

t = 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方法above

import pandas as pd
import numpy as np


def max_and_index(series):
    x, y = series.agg(['max', 'idxmax'])
    return x, y

def 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,000

idx = 5
%%timeit -n 2 -r 10
max_and_index(a[idx])
# 144 ms ± 5.45 ms per loop (mean ± std. dev. of 10 runs, 2 loops each)

%%timeit -n 2 -r 10
max_and_idxmax(a[idx])
# 143 ms ± 5.14 ms per loop (mean ± std. dev. of 10 runs, 2 loops each)

%%timeit -n 2 -r 10
Max_Argmax(a[idx])
# 9.89 ms ± 1.13 ms per loop (mean ± std. dev. of 10 runs, 2 loops each)

%%timeit -n 2 -r 10
np_max_and_argmax(a[idx])
# 24.5 ms ± 1.74 ms per loop (mean ± std. dev. of 10 runs, 2 loops each)