Python 属性错误:';int';对象没有属性';绘图';熊猫

Python 属性错误:';int';对象没有属性';绘图';熊猫,python,pandas,Python,Pandas,我试图可视化每列中的空值,但出现错误:AttributeError:'int'对象没有属性“plot”。 columns_with_null = ['ACCTAGE', 'PHONE', 'POS','INV','INVBAL','POSAMT', 'CC', 'CCBAL','HMOWN' 'CCPURC', 'INCOME', 'LORES', 'HMVAL', 'AGE','CRSCORE'] for col in columns_with_nu

我试图可视化每列中的空值,但出现错误:
AttributeError:'int'对象没有属性“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()

您需要使用空值来传递所有列,而不是
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)
返回数据帧的长度,这里是整数,您需要绘制它。另一个问题是第一部分也返回整数,因为只处理一列-
df[col].isna().sum()
您需要使用空值传递所有列,而不是
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)
返回数据帧的长度,这里是整数,您需要绘制它。另一个问题是第一部分也返回整数,因为只处理一列-
df[col].isna().sum()

这是因为
sum()
返回一个值,它是
df[col]
的所有值之和。现在所有的事情都在处理这个数字

  • 如果要绘制整个数据帧,则
    df[col].plot()
    可能会有所帮助

  • 若您想使用数据帧的平均值,那个么最简单的方法就是打印它

  • 如果仍要打印该单个值,则必须使用外部库,如
    matplotlib

这是因为
sum()
返回一个值,该值是
df[col]
的所有值之和。现在所有的事情都在处理这个数字

  • 如果要绘制整个数据帧,则
    df[col].plot()
    可能会有所帮助

  • 若您想使用数据帧的平均值,那个么最简单的方法就是打印它

  • 如果仍要打印该单个值,则必须使用外部库,如
    matplotlib


能否请您在创建/修改
plt
的地方添加代码?能否请您在创建/修改
plt
的地方添加代码?