pandas读取excel报错

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
File D:\Program Files (x86)\anaconda\envs\ag\lib\site-packages\pandas\compat\_optional.py:141, in import_optional_dependency(name, extra, errors, min_version)
    140 try:
--> 141     module = importlib.import_module(name)
    142 except ImportError:

File D:\Program Files (x86)\anaconda\envs\ag\lib\importlib\__init__.py:127, in import_module(name, package)
    126         level += 1
--> 127 return _bootstrap._gcd_import(name[level:], package, level)

File <frozen importlib._bootstrap>:1030, in _gcd_import(name, package, level)

File <frozen importlib._bootstrap>:1007, in _find_and_load(name, import_)

File <frozen importlib._bootstrap>:984, in _find_and_load_unlocked(name, import_)

ModuleNotFoundError: No module named 'xlrd'

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
Cell In[16], line 1
----> 1 test=pd.read_excel("E:/文档/Projects/jupyter/HR_Pressure_Predict/data/AUO-train/model/AM/worker4/t2原始数据.xls")
      2 test=pd.DataFrame(test)
      3 test.head()

File D:\Program Files (x86)\anaconda\envs\ag\lib\site-packages\pandas\util\_decorators.py:211, in deprecate_kwarg.<locals>._deprecate_kwarg.<locals>.wrapper(*args, **kwargs)
    209     else:
    210         kwargs[new_arg_name] = new_arg_value
--> 211 return func(*args, **kwargs)

File D:\Program Files (x86)\anaconda\envs\ag\lib\site-packages\pandas\util\_decorators.py:331, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs)
    325 if len(args) > num_allow_args:
    326     warnings.warn(
    327         msg.format(arguments=_format_argument_list(allow_args)),
    328         FutureWarning,
    329         stacklevel=find_stack_level(),
    330     )
--> 331 return func(*args, **kwargs)

File D:\Program Files (x86)\anaconda\envs\ag\lib\site-packages\pandas\io\excel\_base.py:482, in read_excel(io, sheet_name, header, names, index_col, usecols, squeeze, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, parse_dates, date_parser, thousands, decimal, comment, skipfooter, convert_float, mangle_dupe_cols, storage_options)
    480 if not isinstance(io, ExcelFile):
    481     should_close = True
--> 482     io = ExcelFile(io, storage_options=storage_options, engine=engine)
    483 elif engine and engine != io.engine:
    484     raise ValueError(
    485         "Engine should not be specified when passing "
    486         "an ExcelFile - ExcelFile already has the engine set"
    487     )

File D:\Program Files (x86)\anaconda\envs\ag\lib\site-packages\pandas\io\excel\_base.py:1695, in ExcelFile.__init__(self, path_or_buffer, engine, storage_options)
   1692 self.engine = engine
   1693 self.storage_options = storage_options
-> 1695 self._reader = self._engines[engine](self._io, storage_options=storage_options)

File D:\Program Files (x86)\anaconda\envs\ag\lib\site-packages\pandas\io\excel\_xlrd.py:34, in XlrdReader.__init__(self, filepath_or_buffer, storage_options)
     24 """
     25 Reader using xlrd engine.
     26 
   (...)
     31 {storage_options}
     32 """
     33 err_msg = "Install xlrd >= 1.0.0 for Excel support"
---> 34 import_optional_dependency("xlrd", extra=err_msg)
     35 super().__init__(filepath_or_buffer, storage_options=storage_options)

File D:\Program Files (x86)\anaconda\envs\ag\lib\site-packages\pandas\compat\_optional.py:144, in import_optional_dependency(name, extra, errors, min_version)
    142 except ImportError:
    143     if errors == "raise":
--> 144         raise ImportError(msg)
    145     else:
    146         return None

ImportError: Missing optional dependency 'xlrd'. Install xlrd >= 1.0.0 for Excel support Use pip or conda to install xlrd.

代码:

test=pd.read_excel("E:/文档/Projects/jupyter/HR_Pressure_Predict/data/AUO-train/model/AM/worker4/t2原始数据.xls")
test=pd.DataFrame(test)
test.head()

pandas读取excel报错_第1张图片
解决办法:
报错信息显示缺少 ‘xlrd’ 依赖,这是因环境中没有安装 xlrd 库导致的。
xlrd 是 pandas 库用于读取 Excel 文件的一个可选依赖项。

要解决这个问题,可以通过运行以下命令来安装 xlrd:
使用 pip 安装:

pip install xlrd

或者
使用 conda 安装(如果您使用的是 Anaconda 或 Miniconda):

conda install -c anaconda xlrd

直接在notebook里面安装
pandas读取excel报错_第2张图片
同理读取xlsx文件报错,采取类似的解决办法:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
File D:\Program Files (x86)\anaconda\envs\ag\lib\site-packages\pandas\compat\_optional.py:141, in import_optional_dependency(name, extra, errors, min_version)
    140 try:
--> 141     module = importlib.import_module(name)
    142 except ImportError:

File D:\Program Files (x86)\anaconda\envs\ag\lib\importlib\__init__.py:127, in import_module(name, package)
    126         level += 1
--> 127 return _bootstrap._gcd_import(name[level:], package, level)

File <frozen importlib._bootstrap>:1030, in _gcd_import(name, package, level)

File <frozen importlib._bootstrap>:1007, in _find_and_load(name, import_)

File <frozen importlib._bootstrap>:984, in _find_and_load_unlocked(name, import_)

ModuleNotFoundError: No module named 'openpyxl'

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
Cell In[4], line 1
----> 1 test=pd.read_excel("E:/文档/Projects/jupyter/HR_Pressure_Predict/data/AUO-train/model/AM/worker4/t2原始数据.xlsx")
      3 test.head()

File D:\Program Files (x86)\anaconda\envs\ag\lib\site-packages\pandas\util\_decorators.py:211, in deprecate_kwarg.<locals>._deprecate_kwarg.<locals>.wrapper(*args, **kwargs)
    209     else:
    210         kwargs[new_arg_name] = new_arg_value
--> 211 return func(*args, **kwargs)

File D:\Program Files (x86)\anaconda\envs\ag\lib\site-packages\pandas\util\_decorators.py:331, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs)
    325 if len(args) > num_allow_args:
    326     warnings.warn(
    327         msg.format(arguments=_format_argument_list(allow_args)),
    328         FutureWarning,
    329         stacklevel=find_stack_level(),
    330     )
--> 331 return func(*args, **kwargs)

File D:\Program Files (x86)\anaconda\envs\ag\lib\site-packages\pandas\io\excel\_base.py:482, in read_excel(io, sheet_name, header, names, index_col, usecols, squeeze, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, parse_dates, date_parser, thousands, decimal, comment, skipfooter, convert_float, mangle_dupe_cols, storage_options)
    480 if not isinstance(io, ExcelFile):
    481     should_close = True
--> 482     io = ExcelFile(io, storage_options=storage_options, engine=engine)
    483 elif engine and engine != io.engine:
    484     raise ValueError(
    485         "Engine should not be specified when passing "
    486         "an ExcelFile - ExcelFile already has the engine set"
    487     )

File D:\Program Files (x86)\anaconda\envs\ag\lib\site-packages\pandas\io\excel\_base.py:1695, in ExcelFile.__init__(self, path_or_buffer, engine, storage_options)
   1692 self.engine = engine
   1693 self.storage_options = storage_options
-> 1695 self._reader = self._engines[engine](self._io, storage_options=storage_options)

File D:\Program Files (x86)\anaconda\envs\ag\lib\site-packages\pandas\io\excel\_openpyxl.py:556, in OpenpyxlReader.__init__(self, filepath_or_buffer, storage_options)
    541 @doc(storage_options=_shared_docs["storage_options"])
    542 def __init__(
    543     self,
    544     filepath_or_buffer: FilePath | ReadBuffer[bytes],
    545     storage_options: StorageOptions = None,
    546 ) -> None:
    547     """
    548     Reader using openpyxl engine.
    549 
   (...)
    554     {storage_options}
    555     """
--> 556     import_optional_dependency("openpyxl")
    557     super().__init__(filepath_or_buffer, storage_options=storage_options)

File D:\Program Files (x86)\anaconda\envs\ag\lib\site-packages\pandas\compat\_optional.py:144, in import_optional_dependency(name, extra, errors, min_version)
    142 except ImportError:
    143     if errors == "raise":
--> 144         raise ImportError(msg)
    145     else:
    146         return None

ImportError: Missing optional dependency 'openpyxl'.  Use pip or conda to install openpyxl.

解决办法:

conda install -c anaconda openpyxl

或者

pip install openpyxl

pandas读取excel报错_第3张图片
pandas读取excel报错_第4张图片

你可能感兴趣的:(Python学习私人笔记,pandas,excel,python)