Understand pandas groupby/apply behaviour
<p>Let's take the following DataFrame:</p>

<pre><code>     location   outlook   play  players  temperature
0     Hamburg     sunny   True     2.00        25.00
1      Berlin     sunny   True     2.00        25.00
2   Stuttgart       NaN   True     4.00        19.00
3         NaN       NaN    NaN      nan          nan
4   Flensburg  overcast  False     0.00        33.00
5    Hannover      rain    NaN     0.00        27.00
6  Heidelberg      rain    NaN     0.00        21.50
7   Frankfurt  overcast   True     2.00        26.00
8    Augsburg     sunny   True     2.00        13.00
9       Koeln     sunny   True     2.00        16.00
</code></pre>

<p>I run</p>

<pre><code>g = df(by=["outlook", "play"])
def gfunc(x):
    print(x)
g.apply(gfunc)
</code></pre>

<p>and this is printed</p>

<pre><code>    location   outlook   play  players  temperature
4  Flensburg  overcast  False     0.00        33.00
    location   outlook   play  players  temperature
4  Flensburg  overcast  False     0.00        33.00
    location   outlook  play  players  temperature
7  Frankfurt  overcast  True     2.00        26.00
   location outlook  play  players  temperature
0   Hamburg   sunny  True     2.00        25.00
1    Berlin   sunny  True     2.00        25.00
8  Augsburg   sunny  True     2.00        13.00
9     Koeln   sunny  True     2.00        16.00
</code></pre>

<p>I don't mind not returning anything; I just want to understand why it prints the exact same output twice and then a couple of different outputs. Shouldn't the output of printing rather be a different subgroup every time? What am I missing?</p>

<p>I.. don't know. It's weird. I'm actually able to replicate the problem. </p>

<p>Note that you have a small mistake, you should write <code>df.groupby(["series"])</code> instead of <code>df(by=["series"])</code>.</p>

<pre><code>import seaborn as sns

iris = sns.load_dataset('iris')
</code></pre>

<p>Now this statement prints a part double. </p>

<pre><code>iris.ix[1:100:10].groupby(["species"]).apply(lambda x: print(len(x), '\n***\n', x))
</code></pre>

<p><em>Output</em></p>

<pre><code>5 
***
     sepal_length  sepal_width  petal_length  petal_width species
1            4.9          3.0           1.4          0.2  setosa
11           4.8          3.4           1.6          0.2  setosa
21           5.1          3.7           1.5          0.4  setosa
31           5.4          3.4           1.5          0.4  setosa
41           4.5          2.3           1.3          0.3  setosa
5 
***
     sepal_length  sepal_width  petal_length  petal_width species
1            4.9          3.0           1.4          0.2  setosa
11           4.8          3.4           1.6          0.2  setosa
21           5.1          3.7           1.5          0.4  setosa
31           5.4          3.4           1.5          0.4  setosa
41           4.5          2.3           1.3          0.3  setosa
5 
***
     sepal_length  sepal_width  petal_length  petal_width     species
51           6.4          3.2           4.5          1.5  versicolor
61           5.9          3.0           4.2          1.5  versicolor
71           6.1          2.8           4.0          1.3  versicolor
81           5.5          2.4           3.7          1.0  versicolor
91           6.1          3.0           4.6          1.4  versicolor
</code></pre>

<p>What is extra weird, is that if I ask for the name, it doesn't double the print. </p>

<pre><code>iris.ix[1:100:10].groupby(["species"]).apply(lambda x: print(len(x), x.name, '\n***\n', x))
</code></pre>

<p><em>Output</em></p>

<pre><code>5 setosa 
***
     sepal_length  sepal_width  petal_length  petal_width species
1            4.9          3.0           1.4          0.2  setosa
11           4.8          3.4           1.6          0.2  setosa
21           5.1          3.7           1.5          0.4  setosa
31           5.4          3.4           1.5          0.4  setosa
41           4.5          2.3           1.3          0.3  setosa
5 versicolor 
***
     sepal_length  sepal_width  petal_length  petal_width     species
51           6.4          3.2           4.5          1.5  versicolor
61           5.9          3.0           4.2          1.5  versicolor
71           6.1          2.8           4.0          1.3  versicolor
81           5.5          2.4           3.7          1.0  versicolor
91           6.1          3.0           4.6          1.4  versicolor
</code></pre>

<p>Well. You got me! Looks like a weird bug. </p>

python pandas