user308827
user308827

Reputation: 21971

Counter in Pandas groupby apply

Is there a way to have a counter variable in the function called through a pandas groupby apply?

def func():
    # Get the counter for how many times this func has been called

df.groupby('A').apply(func)

This is what I am doing right now:

grp = df.groupby('A')
idx = 1
for name, group in grp:
    print name
    func(grp,idx)
    idx += 1

Upvotes: 3

Views: 2724

Answers (1)

Andy Hayden
Andy Hayden

Reputation: 375495

Note: this is an implementation detail, the number of times the function in an apply is called may depend on the return type / whether the apply takes the slow or fast path...

I would count the number of times a function is called by updating an attribute on this function:

In [11]: df = pd.DataFrame([[1, 2], [3, 4]], columns=['A', 'B'])

In [12]: df
Out[12]:
   A  B
0  1  2
1  3  4

In [13]: def f(a):
             f.i += 1
             return a

In [14]: f.i = 0  # set the number of calls to 0

In [15]: g = df.groupby('A')

In [16]: g.apply(f)
Out[16]:
   A  B
0  1  2
1  3  4

In [17]: f.i
Out[17]: 3

As we see, the number of times f is called is 3 (perhaps surprisingly).

To get the number of groups you can use the ngroups attribute:

In [18]: g.ngroups
Out[18]: 2

Upvotes: 4

Related Questions