python-3.xpandaspandas-groupbyiterablemanual-testing

Is there a simple way to manually iterate through existing pandas groupby objects?


Is there a simple way to manually iterate through existing pandas groupby objects?

import pandas as pd

df = pd.DataFrame({'x': [0, 1, 2, 3, 4], 'category': ['A', 'A', 'B', 'B', 'B']})
grouped = df.groupby('category')

In the application a for name, group in grouped: loops follows. For manual-testing I would like to do something like group = grouped[0] and run the code within the for-loop. Unfortunately this does not work. The best thing I could find (here) was

group = df[grouped.ngroup()==0]

which relies on the original DataFrame and not soley on the groupby-Object and is therefore not optimal imo.


Solution

  • Any iterable (here the GroupBy object) can be turned into an iterator:

    group_iter = iter(grouped)
    

    The line below will be the equivalent of selecting the first group (indexed by 0):

    name, group = next(group_iter)
    

    To get the next group, just repeat:

    name, group = next(group_iter)
    

    And so on...


    Source: https://treyhunner.com/2018/02/python-range-is-not-an-iterator/