pandas GroupBy columns with NaN (missing) values pandas GroupBy columns with NaN (missing) values python python

pandas GroupBy columns with NaN (missing) values


pandas >= 1.1

From pandas 1.1 you have better control over this behavior, NA values are now allowed in the grouper using dropna=False:

pd.__version__# '1.1.0.dev0+2004.g8d10bfb6f'# Example from the docsdf   a    b  c0  1  2.0  31  1  NaN  42  2  1.0  33  1  2.0  2# without NA (the default)df.groupby('b').sum()     a  cb        1.0  2  32.0  2  5
# with NAdf.groupby('b', dropna=False).sum()     a  cb        1.0  2  32.0  2  5NaN  1  4


This is mentioned in the Missing Data section of the docs:

NA groups in GroupBy are automatically excluded. This behavior is consistent with R

One workaround is to use a placeholder before doing the groupby (e.g. -1):

In [11]: df.fillna(-1)Out[11]:    a   b0  1   41  2  -12  3   6In [12]: df.fillna(-1).groupby('b').sum()Out[12]:     ab    -1  24   16   3

That said, this feels pretty awful hack... perhaps there should be an option to include NaN in groupby (see this github issue - which uses the same placeholder hack).

However, as described in another answer, "from pandas 1.1 you have better control over this behavior, NA values are now allowed in the grouper using dropna=False"


Ancient topic, if someone still stumbles over this--another workaround is to convert via .astype(str) to string before grouping. That will conserve the NaN's.

df = pd.DataFrame({'a': ['1', '2', '3'], 'b': ['4', np.NaN, '6']})df['b'] = df['b'].astype(str)df.groupby(['b']).sum()
    ab   4   16   3nan 2