AttributeError:“ int”对象在熊猫中没有属性“ plot”

时间:2020-06-07 03:51:23

标签: python pandas

我试图显示每列中的null值,但出现错误:AttributeError: 'int' object has no attribute 'plot'.


columns_with_null = ['ACCTAGE', 'PHONE', 'POS','INV','INVBAL','POSAMT', 'CC', 'CCBAL','HMOWN'
                    'CCPURC', 'INCOME', 'LORES', 'HMVAL', 'AGE','CRSCORE']

for col in columns_with_null:

    print('COLUMN:', col)
    print('percent of nulls:', df[col].isna().sum()/len(df))

    # Viz the value counts
    df[col].isna().sum()/len(df).plot(kind='barh')
    plt.show()

2 个答案:

答案 0 :(得分:0)

您需要将所有带有空值的列传递给col变量,并添加括号或div进行除法:

(df[columns_with_null].isna().sum() / len(df)).plot(kind='barh')

df[columns_with_null].isna().sum().div(len(df)).plot(kind='barh')

如果要绘制所有列:

(df.isna().sum() / len(df)).plot(kind='barh')

df.isna().sum().div(len(df)).plot(kind='barh')

解决方案的问题是一部分:

len(df).plot(kind='barh')

因为len(df)返回了DataFrame的长度,这里是整数,您想绘制它。另一个问题是,第一部分也返回整数,因为仅处理一列-df[col].isna().sum()

答案 1 :(得分:0)

这是因为sum()返回一个单一值,该值是df[col]所有值的总和。现在,所有操作都只处理该单个数字。

  • 如果要绘制整个数据框,那么df[col].plot()可能会帮助

  • 如果您要使用数据框的avg值,那么最简单的方法就是打印出来

  • 如果您仍想绘制单个值,则必须使用matplotlib

  • 之类的外部库
相关问题