在pandas groupby之后删除一个组 [英] Delete a group after pandas groupby

查看:234
本文介绍了在pandas groupby之后删除一个组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从熊猫的groupby对象中删除一个组(按组名)?也就是说,在执行groupby之后,请根据其名称删除结果组.

Is it possible to delete a group (by group name) from a groupby object in pandas? That is, after performing a groupby, delete a resulting group based on its name.

推荐答案

逐组过滤DataFrame已一种更方便的实现方法.

Filtering a DataFrame groupwise has been discussed. And a future release of pandas may include a more convenient way to do it.

但是,目前,我认为这是按名称过滤GroupBy对象grouped并返回其余组的DataFrame的最简洁的方法.

But currently, here is what I believe to be the most succinct way to filter the GroupBy object grouped by name and return a DataFrame of the remaining groups.

df.drop(grouped.get_group(group_name).index)

这是从上面的链接派生的更通用的方法:

And here is a more general method derived from the links above:

df[grouped[0].transform(lambda x: x.name != group_name).astype('bool')]

这篇关于在pandas groupby之后删除一个组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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