为多索引数据框的每个级别查找最小值 [英] Finding minimum value for each level of a multi-index dataframe
本文介绍了为多索引数据框的每个级别查找最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像这样的DataFrame:
I have a DataFrame that looks like this:
data
a b
1 1 0.1
2 0.2
3 0.3
2 1 0.5
2 0.6
3 0.7
,而我想忽略b
级别找到每个a
级别的最小值,因此作为输出,我正在寻找类似
and I want to find the minimum value for each level of a
ignoring the b
level, so as an output I'm looking for something like
a min
1 0.1
2 0.5
推荐答案
The simpliest is use min
with parameter level=0
:
print (df.data.min(level=0).reset_index(name='min'))
a min
0 1 0.1
1 2 0.5
如果需要输出为df
,并且仅输出一列df
:
If need output as df
and only one column df
:
print (df.min(level=0))
data
a
1 0.1
2 0.5
或groupby
在第一级中加上min
:
print (df.groupby(level=0).data.min().reset_index(name='min'))
a min
0 1 0.1
1 2 0.5
这篇关于为多索引数据框的每个级别查找最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文