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'
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.
CodePudding user response:
When using group by, the object returned is a GroupBy
object, which has the groups
attribute that you used. 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
.
The alternative approach is to transform your dataframe to an iterable (df.to_dict()
for example and then iterate over it.