python - 类型错误 : ufunc 'add' did not contain a loop with signature matching types dtype ('S23' ) dtype ('S23' ) dtype ('S23' )

标签 python pandas

我有 pandas 数据框 df,我想打印出变量列表以及类型和缺失字段的数量(NaN、NA)。

def var_desc(df,dt):
    print('============================================')
    for c in df.columns:
        if df[c].dtype==dt:
            e = sum(pd.isnull(df[c]))
            print(c+' : '+dt+' | '+e)
    print('============================================') 

var_desc(df,"object")
var_desc(df,"int64")

e = sum(pd.isnull(df[c])) 存在一些问题。它给出以下错误:

TypeError: ufunc 'add' 不包含签名匹配类型 dtype('S23') dtype('S23') dtype('S23') 的循环

如何解决?

最佳答案

你可以使用

count_nan = len(df) - df.count()

count返回非 NA/null,这样您就可以达到非 None 记录的数量。

DataFrame.count(axis=0, level=None, numeric_only=False)[source]¶

Return Series with number of non-NA/null observations over requested axis. Works with non-floating point data as well (detects NaN and None)

关于python - 类型错误 : ufunc 'add' did not contain a loop with signature matching types dtype ('S23' ) dtype ('S23' ) dtype ('S23' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43784832/

相关文章:

python - 在 Bokeh 楔形图表上正确显示悬停工具提示

python - 通过带内联的 Django ModelAdmin 在管理门户中显示与 "self"的两个关系

python - 如何以非阻塞方式从脚本中使用 Pylab 显示图像

python Pandas : How can I group by and assign an id to all the items in a group?

python - 安装 Pandas 的问题 : "command ' llvm-gcc-4. 2' failed with exit status 1"

python - 将趋势线添加到 matplotlib 线图 python

python - 应用引擎/ python : Why isn't the exception caught?

python - 分离单词并按列计数并将它们分成两个单词

python - 每个组的值的出现次数作为单独的列

python - 编写数据框字典以分隔 Excel 工作表