Code Sample, a copy-pastable example if possible

import pandas as pd
tt=[]

def func(x):
    tt.append(x.B.iloc[0])
    return 1
df = pd.DataFrame({'A': [1,1,1,1,1,2,2,2,2], 'B': [0,1,2,3,4,5,6,7,8]})
df.groupby('A').apply(func)
tt

#### [0, 0, 5]

Problem description

the list 'tt' should be [0, 5], but the first group applied twice and 'tt' become [0, 0, 5]. so is it a bug here?

For documentation-related issues, you can check the latest versions of the docs on master here:

https://pandas-docs.github.io/pandas-docs-travis/

If the issue has not been resolved there, go ahead and file it in the issue tracker.

Expected Output

[0,5]

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.6.1.final.0 python-bits: 64 OS: Linux OS-release: 3.10.0-514.16.1.el7.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: zh_CN.UTF-8 LOCALE: zh_CN.UTF-8 pandas: 0.22.0 pytest: 3.0.7 pip: 9.0.1 setuptools: 38.5.1 Cython: 0.27.3 numpy: 1.14.1 scipy: 1.0.0 pyarrow: 0.8.0 xarray: 0.10.0 IPython: 5.3.0 sphinx: 1.5.6 patsy: 0.4.1 dateutil: 2.6.1 pytz: 2017.3 blosc: None bottleneck: 1.2.1 tables: 3.3.0 numexpr: 2.6.2 feather: 0.4.0 matplotlib: 2.0.2 openpyxl: 2.4.7 xlrd: 1.0.0 xlwt: 1.2.0 xlsxwriter: 1.0.0 lxml: 3.7.3 bs4: 4.6.0 html5lib: 0.999 sqlalchemy: 1.1.9 pymysql: 0.7.11.None psycopg2: None jinja2: 2.10 s3fs: None fastparquet: None pandas_gbq: None pandas_datareader: 0.5.0

Comment From: jorisvandenbossche

You can check the notes here: https://pandas-docs.github.io/pandas-docs-travis/generated/pandas.core.groupby.GroupBy.apply.html