jgg
jgg

Reputation: 831

How to get the number of instances in an itertools._grouper object?

Suppose I have the below dataframe:

>>>from itertools import groupby
>>>import pandas as pd

>>>idx1 = pd.date_range('2019-01-01',periods=5)
>>>idx2 = pd.date_range('2020-06-01',periods=5)
>>>idx3 = pd.date_range('2021-08-15',periods=5)
>>>idx4 = pd.date_range('2022-03-20',periods=5)
>>>idx = idx1.union(idx2).union(idx3).union(idx4)

>>>l = [1,-1,-4,2,-3,4,5,1,-3,-4,-5,-3,-4,2,3,-1,-2,3,2,3]

>>>df = pd.DataFrame(l, index=idx, columns=['a'])
>>>df
            a
2019-01-01  1
2019-01-02 -1
2019-01-03 -4
2019-01-04  2
2019-01-05 -3
2020-06-01  4
2020-06-02  5
2020-06-03  1
2020-06-04 -3
2020-06-05 -4
2021-08-15 -5
2021-08-16 -3
2021-08-17 -4
2021-08-18  2
2021-08-19  3
2022-03-20 -1
2022-03-21 -2
2022-03-22  3
2022-03-23  2
2022-03-24  3

>>>for k,g in groupby(df['a'], lambda x: x<0):
       print(k, sum(g))

False 1
True -5
False 2
True -3
False 10
True -19
False 5
True -3
False 8

How can I get the count of the number of instances in each group? I tried to apply the len() built-in but got the below error:

>>>for k,g in groupby(df['a'], lambda x: x<0):
       print(k,len(g))

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [241], in <cell line: 1>()
      1 for k,g in groupby(df['a'], lambda x: x<0):
----> 2     print(k,len(g))

TypeError: object of type 'itertools._grouper' has no len()

Upvotes: 0

Views: 154

Answers (1)

jgg
jgg

Reputation: 831

Still unsure why len doesn't work but sum does. Nonetheless, the below produces the desired result.

>>>[len(list(g)) for k,g in groupby(df['a'], lambda x: x<0) if k]
[2,1,5,2]

Upvotes: 1

Related Questions