在pandas dataframe groupby对象中访问元组的外部元素

时间:2016-02-02 23:24:08

标签: python pandas

我在multiindex数据帧上创建了一个groupby对象(例如(a,b))。我可以使用df.get_group((a,b))访问内部组。但是,我也希望能够访问外部组,例如df.get_group((a,))。我可以通过df.xs(key,level)命令间接实现这一点,但这不是解决问题的理想方法。有人可以帮我吗?

    df1.get_group(('specialty finance','mortgage finance'))
                                          lasts         prev ticker
industry          sub_industry                                     
specialty finance mortgage finance   190.000000   190.649994   dewh
                  mortgage finance   252.699997   253.350006   grhf
                  mortgage finance  1179.800049  1180.050049   hdfc
                  mortgage finance   693.849976   708.450012   ihfl
                  mortgage finance   450.950012   468.250000  lichf

df1.get_group(('specialty finance',))
ValueError: must supply a a same-length tuple to get_group with multiple grouping keys

1 个答案:

答案 0 :(得分:0)

这取决于您确定static NSString *kUITabBarButtonClassName = @"UITabBarButton"; NSString *prevFocusViewClassName = NSStringFromClass([context.previouslyFocusedView class]); NSString *nextFocusedView = NSStringFromClass([context.nextFocusedView class]); // The tabbar is going to disappear if ([prevFocusViewClassName isEqualToString:kUITabBarButtonClassName] && ![nextFocusedView isEqualToString:kUITabBarButtonClassName]) { [self.view layoutIfNeeded]; self.constraintScrollViewCenterY.constant -= self.tabBarController.tabBar.frame.size.height; [coordinator addCoordinatedAnimations:^{ [self.view layoutIfNeeded]; } completion:nil]; // The tabbar is going to appear } else if (![prevFocusViewClassName isEqualToString:kUITabBarButtonClassName] && [nextFocusedView isEqualToString:kUITabBarButtonClassName]) { [self.view layoutIfNeeded]; self.constraintScrollViewCenterY.constant += self.tabBarController.tabBar.frame.size.height; [coordinator addCoordinatedAnimations:^{ [self.view layoutIfNeeded]; } completion:nil]; } 命名对象的第一个groupby()。如果您只按行业(外部组)在原始多索引数据框架上运行get_group(),则可以单独输出到“专业财务”组:

groupby

或者,您可以继续使用df1 = originaldf.groupby(['industry']).get_group('specialty finance') 对象的所有元素进行当前分组和iterate,甚至可以根据您的特定行业调整分组:

groupby