You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行pandas代码提示'NoneType' object has no attribute 'head'的原因是什么

错误产生原因
  • pandas 中所有支持inplace参数的方法,当设置inplace=True时,方法会直接修改原数据对象,不会返回新的对象,返回值固定为None。
  • 你将df.fillna(0, inplace=True)的返回值赋值给了df_new,此时df_new就是None类型,调用head()方法自然会抛出'NoneType' object has no attribute 'head'的属性错误。
解决方案

有两种可行修改方式,按需选择即可:

  • 方式一:保留inplace=True参数,不接收返回值,直接使用修改后的原df
    对应代码:
    df.fillna(0, inplace=True)
    print(df.head(10))
    
  • 方式二:移除inplace=True参数,接收方法返回的新填充后DataFrame赋值给df_new
    对应代码:
    df_new = df.fillna(0)
    print(df_new.head(10))
    

内容的提问来源于stack exchange,提问作者Zhen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 14:27:03