Python Multiindex Dataframe 删除最大值 [英] Python Multiindex Dataframe remove maximum

查看:155
本文介绍了Python Multiindex Dataframe 删除最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 python pandas 中的 MultiIndex DataFrame 苦苦挣扎.

I am struggling with MultiIndex DataFrame in python pandas.

假设我有一个这样的 df:

Suppose I have a df like this:

                    count    day     
group    name

  A      Anna        10      Monday
         Beatrice    15      Tuesday

  B      Beatrice    15      Wednesday
         Cecilia     20      Thursday

我需要的是找到每个组的名称最大值并将其从数据框中删除.

What I need is to find the maximum in name for each group and remove it from the dataframe.

最终的 df 看起来像:

The final df would look like:

                    count    day     
group    name

  A      Anna        10      Monday

  B      Beatrice    15      Wednesday

你们有没有人知道如何做到这一点?我的想法快用完了...

Does any of you have any idea how to do this? I am running out of ideas...

提前致谢!

如果原始数据帧是:

                   count    day     
group    name

  A      Anna        10      Monday
         Beatrice    15      Tuesday

  B      Beatrice    20      Wednesday
         Cecilia     15      Thursday

并且最终的 df 需要是:

and the final df needs to be:

                    count    day     
group    name

  A      Anna        10      Monday

  B      Beatrice    20      Wednesday

推荐答案

更新:

In [386]: idx = (df.reset_index('name')
                   .groupby('group')['name']
                   .max()
                   .reset_index()
                   .values.tolist())

In [387]: df.loc[df.index.difference(idx)]
Out[387]:
                count        day
group name
A     Anna         10     Monday
B     Beatrice     20  Wednesday

<小时>

In [326]: df.loc[df.index.difference(df.groupby('group')['count'].idxmax())]
Out[326]:
                count        day
group name
A     Anna         10     Monday
B     Beatrice     15  Wednesday

PS 很可能有更好的方法来做到这一点...

PS most probably there is a better way to do this...

这篇关于Python Multiindex Dataframe 删除最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆