从 pandas 多索引数据框中获取最大值 [英] Getting max values from pandas multiindex dataframe
本文介绍了从 pandas 多索引数据框中获取最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图从具有多个索引的熊猫数据框中仅检索最大值(包括多索引值).我拥有的数据帧是通过groupby和列选择('tOfmAJyI')生成的,如下所示:
Im trying to retrieve only the max values (including the multi index values) from a pandas dataframe that has multiple indexes. The dataframe I have is generated via a groupby and column selection ('tOfmAJyI') like this:
df.groupby('id')['tOfmAJyI'].value_counts()
Out[4]:
id tOfmAJyI
3 mlNXN 4
SSvEP 2
hCIpw 2
5 SSvEP 2
hCIpw 1
mlNXN 1
11 mlNXN 2
SSvEP 1
...
我想要实现的是获取最大值,包括其对应的索引值.像这样:
What I would like to achieve is to get the max values including their corresponding index values. So something like:
id tOfmAJyI
3 mlNXN 4
5 SSvEP 2
11 mlNXN 2
...
有什么想法可以实现这一目标吗?我可以获取id和最大值,但是我仍在尝试获取相应的'tOfmAJyI'值.
Any ideas how I can achieve this? I was able to get the id and max value but I'm still trying to get the corresponding value of 'tOfmAJyI'.
推荐答案
groupby
+ head
df.groupby(level=0).head(1)
Out[1882]:
id tOfmAJyI
3 mlNXN 4
5 SSvEP 2
11 mlNXN 2
Name: V, dtype: int64
或
df.loc[df.groupby(level=0).idxmax()]
Out[1888]:
id tOfmAJyI
3 mlNXN 4
5 SSvEP 2
11 mlNXN 2
Name: V, dtype: int64
这篇关于从 pandas 多索引数据框中获取最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文