Python Pandas 报错 'AttributeError: module 'pandas' has no attribute 'nan'' 解决方法
Python Pandas 报错 'AttributeError: module 'pandas' has no attribute 'nan'' 解决方法
在使用 Python Pandas 读取文件时,如果遇到以下错误信息:
Traceback (most recent call last):
File 'C:\Users\HUAWEI\Desktop\123.py', line 12, in <module>
df = df.fillna(value=pd.nan)
AttributeError: module 'pandas' has no attribute 'nan'
这意味着在您使用的 Pandas 版本中,'nan' 属性已经不存在了。这通常是由于 Pandas 版本更新导致的。
为了解决这个问题,您可以尝试以下方法:
-
使用 'np.nan' 替换 'pd.nan'
将代码中的 'pd.nan' 替换为 'np.nan',并在代码开头添加以下导入语句:
import numpy as np这将导入 NumPy 模块并将其命名为 'np',使得您可以使用 'np.nan' 来填充缺失值。
-
检查 Pandas 版本
您可以使用以下代码检查您安装的 Pandas 版本:
import pandas as pd print(pd.__version__)如果您的 Pandas 版本较旧,建议您升级到最新版本,以便使用最新的功能和修复程序。
通过以上方法,您应该能够解决 'AttributeError: module 'pandas' has no attribute 'nan'' 错误并顺利读取文件。
原文地址: https://www.cveoy.top/t/topic/jmEv 著作权归作者所有。请勿转载和采集!