Admittedly, probably not an frequent case scenario. In essence, I am wondering if it is possible to take groups_1=df.groupby('something')
and groups_2=df.groupby('something_else')
and put them into groups = [groups_1,groups_2]
so that I can iterate over one iterable groups object instead of iterating over the two objects separately. Here's an example:
# create data frame
df = pd.DataFrame({'A':np.random.random(20), 'B':np.random.random(20), 'date':pd.date_range("2021-05-01",periods = 20, freq='2W')})
# groupby at two separate frequencies
groups_1 = df.loc[df.date<'2021-11-01',:].groupby(pd.Grouper(key='date', freq='M'))
groups_2 = df.loc[df.date>='2021-11-01',:].groupby(pd.Grouper(key='date', freq='2W'))
Given the above, I would love to do something like:
groups = some_concat_function([groups_1,groups_2])
for label, group in groups:
# do something here
CodePudding user response:
I think you can use itertools.chain
for that:
from itertools import chain
groups = chain(groups_1, groups_2)
...
or
from itertools import chain
groups = chain.from_iterable([groups_1, groups_2])
...