【发布时间】:2018-12-10 10:40:29
【问题描述】:
基本上我想计算由 2 个变量分组的最常见项目的数量。我使用此代码:
dfgrouped = data[COLUMNS.copy()].groupby(['Var1','Var2']).agg(lambda x: stats.mode(x)[1])
此代码有效,但不适用于具有 Nan 值的列,因为 NaN 值是浮点数,而其他值是 str。所以显示这个错误:
'<' not supported between instances of 'float' and 'str'
我想省略 NaN 值和其余的计数模式。所以 str(x) 不是解决方案。并且 scipy.stats.mode(x, nan_policy='omit') 也不起作用,出现错误:
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
请您给我一个建议如何处理它。 谢谢
【问题讨论】:
标签: python pandas dataframe scipy pandas-groupby