Skip to content

Aggregating groupby with multiple functions all return the same value #16904

Closed
@HristoBuyukliev

Description

@HristoBuyukliev

So, I want to aggregate a grouped by object by two criterion: count of observations, and count of nonzero observations:

# nonzero counts by shipment number
data.groupby(['Shipment number'])['Shipment number']
	.agg({'count' : lambda x: x.count(), 
	      'nonzero' : lambda x: x.nonzero()[0].size})
	.sort_values('count')

Problem description

While this code is referenced in issue 7186, and the issue is closed, there is a bug: all the output columns are the same. That is evident even in issue 7186, and I'm shocked how nobody picked it up.

Output of pd.show_versions()

commit: None
python: 2.7.6.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-31-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.20.3
pytest: None
pip: 9.0.1
setuptools: 36.0.1
Cython: None
numpy: 1.13.1
scipy: 0.19.1
xarray: None
IPython: 5.4.1
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.4.1
html5lib: 1.0b3
sqlalchemy: 1.0.14
pymysql: None
psycopg2: 2.7.1 (dt dec pq3 ext lo64)
jinja2: 2.7.3
s3fs: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    GroupbyTestingpandas testing functions or related to the test suite

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions