10

Consider this small example:

data={"X":[1, 2, 3, 4, 5], "Y":[6, 7, 8, 9, 10], "Z": [11, 12, 13, 14, 15])
frame=pd.DataFrame(data,columns=["X","Y","Z"],index=["A","A","A","B","B"])

I want to group frame with

grouped=frame.groupby(frame.index)

Then I want to loop over the groups by:

for group in grouped:

But I'm stuck on the next step: How can I extract the group in each loop as a pandas DataFrame so I can further process it?

feetwet
  • 3,248
  • 7
  • 46
  • 84
Rockbar
  • 1,081
  • 1
  • 20
  • 31
  • Possible duplicate of [How to loop over grouped Pandas dataframe?](https://stackoverflow.com/questions/27405483/how-to-loop-over-grouped-pandas-dataframe) – Deb Aug 21 '17 at 13:50
  • 1
    That is quite similar, but not exactly the same problem. Here, I missed the name-part of the for-command. The other problem is about difference between loop up over or directly use a summary function. – Rockbar Aug 21 '17 at 14:38

2 Answers2

9

df.groupby returns an iterable of 2-tuples: the index, and the group. You can iterate over each group like this:

for _, g in frame.groupby(frame.index):
    .... # do something with `g`

However, if you want to perform some operation on the groups, there are probably better ways than iteration.

cs95
  • 379,657
  • 97
  • 704
  • 746
4

Here is an example:

groups = frame.groupby(level=0)

for n,g in groups:
    print('This is group '+ str(n)+'.')
    print(g)
    print('\n')

Output:

This is group A.
   X  Y   Z
A  1  6  11
A  2  7  12
A  3  8  13


This is group B.
   X   Y   Z
B  4   9  14
B  5  10  15
Scott Boston
  • 147,308
  • 15
  • 139
  • 187