Skip to content

BUG: Output all columns even if subsetting columns after groupby #37737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
nagatanik opened this issue Nov 10, 2020 · 1 comment
Closed
3 tasks done

BUG: Output all columns even if subsetting columns after groupby #37737

nagatanik opened this issue Nov 10, 2020 · 1 comment
Labels
Apply Apply, Aggregate, Transform, Map Bug Groupby Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.).

Comments

@nagatanik
Copy link

nagatanik commented Nov 10, 2020

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Problem

I would like create a dictionary from pandas dataframe using groupby, which the key is the group which is used in pandas groupby and the value is list of the groupby output.

# create a dataframe 
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [2, 3, 4], 'C': [3, 4, 5]})

When subsetting a single column after groupby, it works well,

# get a single column after groupby
dict(df.groupby('A')['B'].apply(list))

# output
{1: [2], 2: [3], 3: [4]}

but when subsetting multiple columns, it outputs the list of dataframe.columns.

# get multiple columns after groupby
dict(df.groupby('A')[['B', 'C']].apply(list))

# output
{1: ['B', 'C'], 2: ['B', 'C'], 3: ['B', 'C']}

I checked raw output when subsetting columns after groupby and found that it outputs all columns even if subsetting columns after groupby.

# get raw output when subsetting columns after groupby
list(df.groupby('A')[['B', 'C']])

# output
[(1,
     A  B  C
  0  1  2  3),
 (2,
     A  B  C
  1  2  3  4),
 (3,
     A  B  C
  2  3  4  5)]

Expected Output

For 1st example:

# get multiple columns after groupby
dict(df.groupby('A')[['B', 'C']].apply(list))

# output
{1: [2, 3], 2: [3, 4], 3: [4, 5]}

And for 2nd example:

# get raw output when subsetting columns after groupby
list(df.groupby('A')[['B', 'C']])

# output
[(1,
     B  C
  0  2  3),
 (2,
     B  C
  1  3  4),
 (3,
     B  C
  2  4  5)]

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2a7d332
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-29-generic
Version : #31-Ubuntu SMP Tue Jul 17 15:39:52 UTC 2018
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8

pandas : 1.1.2
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 47.1.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.18.1
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : 0.8.4
fastparquet : 0.4.1
gcsfs : None
matplotlib : 3.3.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 0.16.0
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.5.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : 0.51.2

@nagatanik nagatanik added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 10, 2020
@mroeschke mroeschke added Apply Apply, Aggregate, Transform, Map Groupby Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.). labels Aug 14, 2021
@mroeschke mroeschke removed the Needs Triage Issue that has not been reviewed by a pandas team member label Aug 23, 2021
@rhshadrach
Copy link
Member

apply consistently acts on the entire DataFrame. The output of list(df) is the columns, and the output of list(ser) is the values. From a groupby perspective, you're getting what I think are the expected results. There is already an issue whether or not applying list to a DataFrame vs a Series is something that should be changed. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Bug Groupby Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.).
Projects
None yet
Development

No branches or pull requests

3 participants