pandas groupby ignoring NAs

余生长醉 提交于 2019-12-13 04:37:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!