<h4 dir="auto">Code Sample, a copy-pastable example if possible</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="# Your code here
import pandas as pd
df = pd.DataFrame({'animal': 'cat dog cat fish dog cat cat'.split(),'size': list('SSMMMLL'),'weight': [8, 10, 11, 1, 20, 12, 12],'adult' : [False] * 5 + [True] * 2})
gb = df.groupby(['animal'])
def GrowUp(x):
    avg_weight = sum(x[x['size'] == 'S'].weight * 1.5)
    avg_weight += sum(x[x['size'] == 'M'].weight * 1.25)
    avg_weight += sum(x[x['size'] == 'L'].weight)
    avg_weight /= len(x)
    print(x)
    return pd.Series(['L',avg_weight,True], index=['size', 'weight', 'adult'])

expected_df = gb.apply(GrowUp)

print(expected_df)"><pre class="notranslate"><span class="pl-c"># Your code here</span>
<span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>
<span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>({<span class="pl-s">'animal'</span>: <span class="pl-s">'cat dog cat fish dog cat cat'</span>.<span class="pl-en">split</span>(),<span class="pl-s">'size'</span>: <span class="pl-en">list</span>(<span class="pl-s">'SSMMMLL'</span>),<span class="pl-s">'weight'</span>: [<span class="pl-c1">8</span>, <span class="pl-c1">10</span>, <span class="pl-c1">11</span>, <span class="pl-c1">1</span>, <span class="pl-c1">20</span>, <span class="pl-c1">12</span>, <span class="pl-c1">12</span>],<span class="pl-s">'adult'</span> : [<span class="pl-c1">False</span>] <span class="pl-c1">*</span> <span class="pl-c1">5</span> <span class="pl-c1">+</span> [<span class="pl-c1">True</span>] <span class="pl-c1">*</span> <span class="pl-c1">2</span>})
<span class="pl-s1">gb</span> <span class="pl-c1">=</span> <span class="pl-s1">df</span>.<span class="pl-en">groupby</span>([<span class="pl-s">'animal'</span>])
<span class="pl-k">def</span> <span class="pl-v">GrowUp</span>(<span class="pl-s1">x</span>):
    <span class="pl-s1">avg_weight</span> <span class="pl-c1">=</span> <span class="pl-en">sum</span>(<span class="pl-s1">x</span>[<span class="pl-s1">x</span>[<span class="pl-s">'size'</span>] <span class="pl-c1">==</span> <span class="pl-s">'S'</span>].<span class="pl-s1">weight</span> <span class="pl-c1">*</span> <span class="pl-c1">1.5</span>)
    <span class="pl-s1">avg_weight</span> <span class="pl-c1">+=</span> <span class="pl-en">sum</span>(<span class="pl-s1">x</span>[<span class="pl-s1">x</span>[<span class="pl-s">'size'</span>] <span class="pl-c1">==</span> <span class="pl-s">'M'</span>].<span class="pl-s1">weight</span> <span class="pl-c1">*</span> <span class="pl-c1">1.25</span>)
    <span class="pl-s1">avg_weight</span> <span class="pl-c1">+=</span> <span class="pl-en">sum</span>(<span class="pl-s1">x</span>[<span class="pl-s1">x</span>[<span class="pl-s">'size'</span>] <span class="pl-c1">==</span> <span class="pl-s">'L'</span>].<span class="pl-s1">weight</span>)
    <span class="pl-s1">avg_weight</span> <span class="pl-c1">/=</span> <span class="pl-en">len</span>(<span class="pl-s1">x</span>)
    <span class="pl-en">print</span>(<span class="pl-s1">x</span>)
    <span class="pl-k">return</span> <span class="pl-s1">pd</span>.<span class="pl-v">Series</span>([<span class="pl-s">'L'</span>,<span class="pl-s1">avg_weight</span>,<span class="pl-c1">True</span>], <span class="pl-s1">index</span><span class="pl-c1">=</span>[<span class="pl-s">'size'</span>, <span class="pl-s">'weight'</span>, <span class="pl-s">'adult'</span>])

<span class="pl-s1">expected_df</span> <span class="pl-c1">=</span> <span class="pl-s1">gb</span>.<span class="pl-en">apply</span>(<span class="pl-v">GrowUp</span>)

<span class="pl-en">print</span>(<span class="pl-s1">expected_df</span>)</pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">From output printed, 'cat' group was printed twice</p>
<p dir="auto">adult animal size  weight<br>
0  False    cat    S       8<br>
2  False    cat    M      11<br>
5   True    cat    L      12<br>
6   True    cat    L      12<br>
adult animal size  weight<br>
0  False    cat    S       8<br>
2  False    cat    M      11<br>
5   True    cat    L      12<br>
6   True    cat    L      12<br>
adult animal size  weight<br>
1  False    dog    S      10<br>
4  False    dog    M      20<br>
adult animal size  weight<br>
3  False   fish    M       1<br>
size   weight  adult<br>
animal<br>
cat       L  12.4375   True<br>
dog       L  20.0000   True<br>
fish      L   1.2500   True<br>
[this should explain <strong>why</strong> the current behaviour is a problem and why the expected output is a better solution.]<br>
'cat' group should only call .apply() once<br>
<strong>Note</strong>: We receive a lot of issues on our GitHub tracker, so it is very possible that your issue has been posted before. Please check first before submitting so that we do not have to handle and close duplicates!</p>
<p dir="auto"><strong>Note</strong>: Many problems can be resolved by simply upgrading <code class="notranslate">pandas</code> to the latest version. Before submitting, please check if that solution works for you. If possible, you may want to check if <code class="notranslate">master</code> addresses this issue, but that is not necessary.</p>
<p dir="auto">For documentation-related issues, you can check the latest versions of the docs on <code class="notranslate">master</code> here:</p>
<p dir="auto"><a href="https://pandas-docs.github.io/pandas-docs-travis/" rel="nofollow">https://pandas-docs.github.io/pandas-docs-travis/</a></p>
<p dir="auto">If the issue has not been resolved there, go ahead and file it in the issue tracker.</p>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">adult animal size  weight<br>
0  False    cat    S       8<br>
2  False    cat    M      11<br>
5   True    cat    L      12<br>
6   True    cat    L      12<br>
adult animal size  weight<br>
1  False    dog    S      10<br>
4  False    dog    M      20<br>
adult animal size  weight<br>
3  False   fish    M       1<br>
size   weight  adult<br>
animal<br>
cat       L  12.4375   True<br>
dog       L  20.0000   True<br>
fish      L   1.2500   True</p>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
<p dir="auto">[paste the output of <code class="notranslate">pd.show_versions()</code> here below this line]</p>
</details>
INSTALLED VERSIONS
------------------
commit: None
python: 3.6.4.final.0
python-bits: 32
OS: Windows
OS-release: 7
machine: x86
processor: x86 Family 6 Model 42 Stepping 7, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: Chinese (Simplified)_People's Republic of China.936
<p dir="auto">pandas: 0.22.0<br>
pytest: None<br>
pip: 9.0.1<br>
setuptools: 28.8.0<br>
Cython: None<br>
numpy: 1.14.0<br>
scipy: None<br>
pyarrow: None<br>
xarray: None<br>
IPython: None<br>
sphinx: None<br>
patsy: None<br>
dateutil: 2.6.1<br>
pytz: 2017.3<br>
blosc: None<br>
bottleneck: None<br>
tables: None<br>
numexpr: None<br>
feather: None<br>
matplotlib: 2.1.1<br>
openpyxl: None<br>
xlrd: None<br>
xlwt: None<br>
xlsxwriter: None<br>
lxml: None<br>
bs4: None<br>
html5lib: None<br>
sqlalchemy: None<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: None<br>
s3fs: None<br>
fastparquet: None<br>
pandas_gbq: None<br>
pandas_datareader: None</p>
<p dir="auto">Using the latest Pandas "pandas (0.13.0-246-g1e1907c)", one critical bug is still existed.</p>
<p dir="auto">For instance, if tuples has some results from SQL and is converted to Dataframe, the first row of the tuples is added "twice" times into Dataframe as below.</p>
<p dir="auto">qr =   # number of records : 100<br>
df = pd.DataFrame.from_records(qr)    # number of records : 101</p>
<p dir="auto">In the log, you can find the first and second row have been duplicated.</p>
<p dir="auto"><em>redacted</em></p>
<p dir="auto">As a result, diggling down the source code "frame.py", some parts caused this issue.</p>
<p dir="auto">Based on the latest version "frame.py", the line number is around 756.</p>
<p dir="auto">Whatever happened, the list "values" has [first_row] around 756 line, and then the whole "data" is added into "value" list again around 760 ~ 769 line.</p>
<p dir="auto">It make the first and second row duplicated.</p>
<p dir="auto">Could you please fix this issue and then update the master branch as well ?</p>
<p dir="auto">751             dtype = None<br>
752             if hasattr(first_row, 'dtype') and first_row.dtype.names:<br>
753                 print("hasattr dtype")<br>
754                 dtype = first_row.dtype<br>
755<br>
756             #values = [first_row]   ## caused the duplicated first and second row<br>
757             values = []    ## should be fixed<br>
758<br>
759             # if unknown length iterable (generator)<br>
760             if nrows is None:<br>
761                 # consume whole generator<br>
762                 values += list(data)<br>
763             else:<br>
764                 #i = 1  ## caused the duplicated first and second row<br>
i = 0 ## should be fixed<br>
765                 for row in data:<br>
766                     values.append(row)<br>
767                     i += 1<br>
768                     if i &gt;= nrows:<br>
769                         break</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/d96ab0ce0ba2c091d33358bed1a1f73ec79659c53a6c4da3eff6816602d31773/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f363435323435322f313935363132352f34383866343937612d383166332d313165332d386536352d6563313661623664373964312e706e67"><img src="https://camo.githubusercontent.com/d96ab0ce0ba2c091d33358bed1a1f73ec79659c53a6c4da3eff6816602d31773/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f363435323435322f313935363132352f34383866343937612d383166332d313165332d386536352d6563313661623664373964312e706e67" alt="2014-01-21 01 51 42" data-canonical-src="https://f.cloud.github.com/assets/6452452/1956125/488f497a-81f3-11e3-8e65-ec16ab6d79d1.png" style="max-width: 100%;"></a></p>