question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GroupBy(axis=1) Does Not Offer Implicit Selection By Columns Name(s)

See original GitHub issue

Code Sample, a copy-pastable example if possible

import pandas as pd
import numpy as np

df = pd.DataFrame(np.arange(12).reshape(3, 4), index=[0, 1, 0], columns=[10, 20, 10, 20])
df.index.name = "y"
df.columns.name = "x"

print df

print
print "Grouped along index:"
print df.groupby(by="y").sum()

print
print "Grouped along columns:"
# The following raises a KeyError even though  "x" is a column name
# (like "y" above, which is an index name):
df.groupby(by="x", axis=1).sum()

Problem description

The exception at the end is surprising: the intent is clearly to group by columns, on the “x” column label.

Furthermore, the documentation for groupby() seems to confirm this, as it states for the “by” argument that “A str or list of strs may be passed to group by the columns in self”.

Expected Output

A dataframe with index [0, 1, 0] but grouped (and summed) columns [10, 20].

I wasn’t able to test with the latest Pandas version, sorry!

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None python: 2.7.13.final.0 python-bits: 64 OS: Linux OS-release: 2.6.32-642.15.1.el6.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: None.None

pandas: 0.21.1 pytest: 3.2.3 pip: 9.0.1 setuptools: 28.8.0 Cython: 0.27.3 numpy: 1.13.3 scipy: 0.19.1 pyarrow: None xarray: 0.8.2 IPython: 5.1.0 sphinx: 1.4.4 patsy: 0.4.1 dateutil: 2.6.1 pytz: 2017.2 blosc: None bottleneck: None tables: 3.4.4 numexpr: 2.6.5 feather: None matplotlib: 2.1.2 openpyxl: None xlrd: None xlwt: None xlsxwriter: None lxml: None bs4: 4.6.0 html5lib: 0.9999999 sqlalchemy: 1.2.18 pymysql: None psycopg2: None jinja2: 2.8 s3fs: None fastparquet: None pandas_gbq: None pandas_datareader: None

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
topper-123commented, Jul 26, 2019

I’ve never understood by to only be usable for index names, and because groupby allows axis=1, it would make sense to me that by follows the supplied axis, and not necessarily only follows the index.

So I see this as bug, given that axis=1 should give the same functionality as axis=0, but onlyshould work over the other axis?

1reaction
topper-123commented, Jul 26, 2019

The example that @lebigot shows, should return the same as df.T.groupby(by="x").sum().T and should return

>>> df.T.groupby(by="x").sum().T  # same as df.groupby(by="x", axis=1).sum()
x  10  20
y
0   2   4
1  10  12
0  18  20

Seems like a bug in groupby to me.

@lebigot if you’d want to investigate, that’d be great.

Read more comments on GitHub >

github_iconTop Results From Across the Web

pandas aggregating columns with groupby axis=1 gives ...
Does anyone know how to aggregate columns by values in a row? You have two r3 rows.
Read more >
What's new in 1.4.0 (January 22, 2022) - Pandas
read_csv() no longer renames unique column labels which conflict with the target names of duplicated columns. Already existing columns are skipped, i.e. the ......
Read more >
SQL GROUP BY - Everything You Need To Know - Sisense
A query select statement can have a column name changed and continue to run, producing an unexpected result. On the other hand, it...
Read more >
Pandas GroupBy - GeeksforGeeks
The abstract definition of grouping is to provide a mapping of labels to group names. Pandas datasets can be split into any of...
Read more >
SQL Group By Tutorial: Count, Sum, Average, and Having ...
The GROUP BY clause is a powerful but sometimes tricky statement to think about. Even eight years later, every time I use a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found