如何从 Pandas 系列中获取最大值和名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46577525/
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
How to get max value and name from a Pandas series?
提问by Abe Miessler
Say I have a series like the one below:
假设我有一个像下面这样的系列:
mySeries = pd.Series([1,2,3],['c','b','a'])
How do I go about getting the max value along with the name associated with it in a single line? In this case: a: 3
如何在一行中获取最大值以及与其关联的名称?在这种情况下:a: 3
I can get the max value with: mySeries.max()
, the name of the max value with mySeries.idxmax(axis=1)
but I can't figure out how to get both of those values with one line. Suggestions?
我可以得到最大值:mySeries.max()
,最大值的名称,mySeries.idxmax(axis=1)
但我不知道如何用一行来获得这两个值。建议?
回答by piRSquared
回答by Bharath
One with boolean indexing (just an alternative) i.e
一个布尔索引(只是一种替代)即
mySeries[mySeries.index==mySeries.idxmax()]
or
或者
mySeries[mySeries == mySeries.max()]
or(Thanks @piRSquared)
或(感谢@piRSquared)
mySeries[[mySeries.idxmax()]]
Output:
输出:
a 3 dtype: int64