[英]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.