我试图显示每列中的空值,但出现错误: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()
这是因为sum()
返回一个单一值,该值是df[col]
所有值的总和。现在,所有事情都只处理该单个数字。
如果要绘制整个数据框,则df[col].plot()
可能会帮助
如果要使用数据框的avg值,那么最简单的方法是打印它
如果您仍想绘制单个值,则必须使用外部库,例如matplotlib
您需要传递所有包含空值的列而不是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()
。