Skip to content

BUG: groupby dropna for multiindex dataframe  #36914

Closed
@AgnesBaud

Description

  • 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.


Hello,

I'm not sure if it's a bug report, a feature request, or if there's already another way to do it. In which case, I would be happy to hear it and close this issue.

Code Sample, a copy-pastable example

import pandas as pd
import numpy as np

test_df = pd.DataFrame.from_dict({
'gene_1' : ['Bacteria', 'Lactobacillus', 11, 17],
'gene_2' : ['Bacteria', 'Lactobacillus', 9, 3],
'gene_3' : [np.nan, np.nan, 1, 2],
'gene_4' : [np.nan, np.nan, 3, 4],
'gene_5' : ['Bacteria', np.nan, 10, 11]
},
orient='index', columns=['kingdom', 'genus', 'sample_1', 'sample_2'])
test_df = test_df.set_index(['kingdom', 'genus'])

test_df

test_df.groupby(level=[0,1], dropna = False).sum()

Current output

groupby sum

Problem description

I would like the dropna option of groupby to handle multipleindex dataframe

Expected Output

whatiwant

Output of pd.show_versions()

INSTALLED VERSIONS

commit : db08276
python : 3.7.3.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-48-generic
Version : #52-Ubuntu SMP Thu Sep 10 10:58:49 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.3
numpy : 1.19.2
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.3
setuptools : 40.8.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 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Related issues : #3729 #29716

Thank you,

Agnès

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions