问题
I have a DataFrame which has a lot of NAs. pandas's groupby operation is ignoring any combinations with NA in it. Is there a way to include NAs in groups? If not, what are the alternatives to pandas groupby? I really don't want to fill in NAs because the fact that something is missing is useful information.
Edit: I noticed that my question is exactly the same issue reported in groupby columns with NaN (missing) values Has there been any developments technology to get around this issue?
回答1:
I will use some kind of non-NA representation for NA only for groupby, which can't be confused with proper data (e.g. -999999 or 'missing')
df.fillna(-999999).groupby(...)
As inplace
argument has default value False
your original dataframe will not be affected.
来源:https://stackoverflow.com/questions/31866781/pandas-groupby-ignoring-nas