简体   繁体   English

Pandas .groups AttributeError: 'DataFrame' 没有属性 'groups

[英]Pandas .groups AttributeError: 'DataFrame' has no attribute 'groups

I have a grouped DataFrame and I'm wanting to get the groups so I can iterate over them later, but .groups is returning AttributeError: 'DataFrame' object has no attribute 'groups'我有一个分组的 DataFrame,我想要获取这些组,以便以后可以对其进行迭代,但是 .groups 正在返回AttributeError: 'DataFrame' object has no attribute 'groups'

test = temp.groupby(['global company key','calendar year'])[['columnA','columnB', 'columnC']].sum()

Example of data:数据示例:

                                   columnA     columnB     columnC
global company key  calendar year  
          1109          2010            20          30          20
          1109          2011            11          10          13
          1468          2010            50          10          16
          1468          2011             1           3           7

What I tried running:我尝试运行的内容:

test.groups

Am I doing something wrong?难道我做错了什么? From the documentation, this should be extremely easy.从文档来看,这应该非常容易。

When using group by, the object returned is a GroupBy object, which has the groups attribute that you used.使用 group by 时,返回的对象是GroupBy对象,它具有您使用的groups属性。 BUT when applying any function on top of the Groupby object (sum() in your case) it changes back to a pd.DataFrame which does not have the .groups .但是,当在Groupby对象(在您的情况下为 sum() )上应用任何函数时,它会变回没有pd.DataFrame.groups

The alternative approach is to transform your dataframe to an iterable ( df.to_dict() for example and then iterate over it.另一种方法是将您的数据框转换为可迭代的(例如df.to_dict() ,然后对其进行迭代。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:AttributeError:'NoneType'对象没有属性'groups' - Python: AttributeError: 'NoneType' object has no attribute 'groups' AttributeError:'NoneType'对象在正则表达式中没有属性'groups' - AttributeError: 'NoneType' object has no attribute 'groups' in Regex 正则表达式:AttributeError:'NoneType' object 没有属性'groups' - Regex: AttributeError: 'NoneType' object has no attribute 'groups' AttributeError: 'ParquetFile' object 没有属性 'row_groups' - AttributeError: 'ParquetFile' object has no attribute 'row_groups' AttributeError: 'NoneType' 对象在 odoo 13 中没有属性 'groups' - AttributeError: 'NoneType' object has no attribute 'groups' in odoo 13 AttributeError: 'NoneType' 对象没有属性 'groups' - re.search - AttributeError: 'NoneType' object has no attribute 'groups' - re.search AttributeError:'NoneType'对象没有尝试循环的属性'groups' - AttributeError: 'NoneType' object has no attribute 'groups' attempting a loop AttributeError:“元组”对象没有属性“权限”。 DJango团体 - AttributeError: 'tuple' object has no attribute 'permissions'. DJango groups Pyinstaller 抛出 AttributeError: 'NoneType' object has no attribute 'groups' 错误 - Pyinstaller throwing AttributeError: 'NoneType' object has no attribute 'groups' error Pandas AttributeError: 'DataFrame' 对象没有属性 'Datetime' - Pandas AttributeError: 'DataFrame' object has no attribute 'Datetime'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM