首页 > 解决方案 > 访问数据框中的列以通过 for 循环验证 dtype 时出现 AttributeError

问题描述

我正在尝试替换数据框中的 nan ,因此我需要访问 float 或 integers 列:它可以在不使用 for 循环的情况下工作

for col in df:
    if(df.col.dtype!="object"):
        aux=df[col].mean() 
        df[col].fillna(aux,inplace=True)

我得到的错误:

AttributeError:“DataFrame”对象没有属性“col”

标签: pythonpandasdtype

解决方案


正确的语法是df[col].dtype


推荐阅读