Python 系列max和idxmax
我试图得到一个系列对象的最大值以及相应的索引Python 系列max和idxmax,python,python-3.x,pandas,Python,Python 3.x,Pandas,我试图得到一个系列对象的最大值以及相应的索引 s=pd.系列(np.random.randn(5),索引=['a','b','c','d','e']) s、 max()将返回最大值,而s.idxmax()将返回最大值的索引。 是否有一种方法可以让我们获得该值及其对应的索引 谢谢。自定义函数怎么样?差不多 import numpy as np import pandas as pd s = pd.Series(np.random.randn(5), index=['a', 'b', 'c', '
s=pd.系列(np.random.randn(5),索引=['a','b','c','d','e'])
s、 max()将返回最大值,而s.idxmax()将返回最大值的索引。
是否有一种方法可以让我们获得该值及其对应的索引
谢谢。自定义函数怎么样?差不多
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)
我在我的电脑上运行它,我得到:
>>> s
a -1.854440
b 0.302282
c -0.630175
d -1.012799
e 0.239437
dtype: float64
>>> max
0.3022819091746019
>>> index
'b'
希望有帮助 自定义函数怎么样?差不多
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)
我在我的电脑上运行它,我得到:
>>> s
a -1.854440
b 0.302282
c -0.630175
d -1.012799
e 0.239437
dtype: float64
>>> max
0.3022819091746019
>>> index
'b'
希望有帮助 正如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'>
如果需要速度,请使用numpy方法
正如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'>
如果需要速度,请使用numpy方法
你认为s1=s[s==val]?类似于:
s.agg(['max','idxmax'])
?尽管。。。idx=s.idxmax()没有问题;val=s[idx]
。@jezrael不确定这是一个像样的骗局。。。这更倾向于过滤……你认为s1=s[s==val]?类似于:s.agg(['max','idxmax'))
?尽管。。。idx=s.idxmax()没有问题;val=s[idx]
。@jezrael不确定这是一个像样的骗局。。。这更倾向于过滤…下降到NumPy有好处吗。而且,如果目的是为了性能,为什么我们(有效地)通过max
+argmax
)计算两次最大值(一个2通解决方案)?hi@jpp!谢谢你的评论。现在呢?我取下了np.max线条当然,这样更好。但是看看与idx=s.idxmax()相比是否有任何显著的优势;val=s[idx]
[正如@JonClements所建议的那样],我建议您在一个大数据帧上计时,例如,%timeit
,否则这看起来有点冗长。我在下面的回答中添加了一些计时,这种方式肯定更快。使用NumPy有好处吗。而且,如果目的是为了性能,为什么我们(有效地)通过max
+argmax
)计算两次最大值(一个2通解决方案)?hi@jpp!谢谢你的评论。现在呢?我取下了np.max线条当然,这样更好。但是看看与idx=s.idxmax()相比是否有任何显著的优势;val=s[idx]
[正如@JonClements建议的那样],我建议您在一个大数据帧上计时,例如,%timeit
,否则这看起来有点冗长。我在下面的回答中添加了一些计时,这种方式肯定更快。