<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; def applym(df):
...     print df.irow(0)['a']
...     return DataFrame({'a':[1],'b':[1]})

&gt;&gt;&gt; df = DataFrame({'a':[1,1,1,2,3],'b':['a','a','a','b','c']})

&gt;&gt;&gt; df.groupby('b').apply(applym)
1
1
2
3
     a  b
b        
a 0  1  1
b 0  1  1
c 0  1  1
&gt;&gt;&gt;"><pre class="notranslate"><code class="notranslate">&gt;&gt;&gt; def applym(df):
...     print df.irow(0)['a']
...     return DataFrame({'a':[1],'b':[1]})

&gt;&gt;&gt; df = DataFrame({'a':[1,1,1,2,3],'b':['a','a','a','b','c']})

&gt;&gt;&gt; df.groupby('b').apply(applym)
1
1
2
3
     a  b
b        
a 0  1  1
b 0  1  1
c 0  1  1
&gt;&gt;&gt;
</code></pre></div>
<p dir="auto">applym is called twice on the first group.</p>
<p dir="auto">It seems that the apply method of a group invokes the function passed as parameter once for each group, as expectes. However, the function is invoked twice for the first group. Enclose is a short code reproducing this behavior. Is it a normal behavior or am I doing something wrong here.</p>
<p dir="auto">I run the code with pandas 0.10.0 on. Windows 7, 32 bit version.</p>
<p dir="auto">The code reproducing this  behavior:</p>
<p dir="auto">import pandas as pd</p>
<p dir="auto">tdf      = pd.DataFrame( { 'cat' : [1,1,1,2,2,1,1,2], 'B' : range(8)})<br>
category = tdf['cat']<br>
pGroups = tdf.groupby(by=category)</p>
<p dir="auto">def getLen(df) :<br>
print  'len ',len(df)<br>
return len(df)</p>
<p dir="auto">plen = pGroups.apply(getLen)</p>