我使用Python中的groupby按地区和加入年份对客户数量进行了分组。但是,我想从区域组中删除几个区域。你知道吗
我知道要从groupby
中排除一个组,可以使用以下代码:
grouped = df.groupby(['Region'])
df1 = df.drop(grouped.get_group(('Southwest')).index).
因此,我最初尝试了以下方法:
grouped = df.groupby(['Region'])
df1 = df.drop(grouped.get_group(('Southwest','Northwest')).index)
然而,这给了我一个明显的错误('Southwest','Northwest')
。你知道吗
现在我想知道是否有一种方法可以一次删除多个组,而不必为每个要删除的区域重复键入上面的代码。你知道吗
不是
df1 = df.drop(grouped.get_group(('Southwest','Northwest')).index)
。grouped.get_group
以单个名称作为参数。如果要删除多个组,可以使用df1 = df.drop((grouped.get_group('Southwest').index, grouped.get_group('Northwest').index))
,因为drop
可以将列表作为输入。你知道吗作为旁注,
('Southwest')
的计算结果是'Southwest'
(即它不是元组)。如果要生成大小为1的元组,它是('Southwest', )
相关问题 更多 >
编程相关推荐