【问题标题】:issue with pandas when launching a python script启动 python 脚本时 pandas 的问题
【发布时间】:2021-03-21 17:00:33
【问题描述】:

我正在使用 Jupyter Notebook 从 xlsx 文件创建图表。 代码有效并且图表在 Jupyter 上正确显示,但是当我尝试从 CLI 启动脚本时,我得到了这个结果:

λ python Untitled.py

Traceback (most recent call last):<br/> File "Untitled.py", line 25, in <module><br/> 
 df = pd.read_excel(workbook)<br/> File
 "C:\Python\lib\site-packages\pandas\util\_decorators.py", line 299, in
 wrapper<br/>   return func(*args, **kwargs)<br/> File
 "C:\Python\lib\site-packages\pandas\io\excel\_base.py", line 336, in
 read_excel<br/>   io = ExcelFile(io, storage_options=storage_options,
 engine=engine)<br/> File
 "C:\Python\lib\site-packages\pandas\io\excel\_base.py", line 1131, in
 __init__<br/>   self._reader = self._engines[engine](self._io, storage_options=storage_options)<br/> File
"C:\Python\lib\site-packages\pandas\io\excel\_xlrd.py", line 24, in
 __init__<br/>   import_optional_dependency("xlrd", extra=err_msg)<br/> File "C:\Python\lib\site-packages\pandas\compat\_optional.py", line
 109, in import_optional_dependency   raise ImportError(msg) from
 None<br/> ImportError: Missing optional dependency 'xlrd'. Install
 xlrd >= 1.0.0 for Excel support Use pip or conda to install xlrd.

代码如下:

import pandas as pd
import matplotlib.pyplot as plt

workbook = "sample_scores.xlsx"

df = pd.read_excel(workbook)
print(df.head())

values = df[['Name','Test 1']]
print (values)

ax = values.plot.bar(x='Name', y='Test 1')
plt.show()

我安装了 pandas 和 matplotlib 包,还验证了 Python 路径,一切都设置正确,但我似乎遗漏了什么?

【问题讨论】:

  • 只需安装xlrd 模块
  • 使用 pip install xlrd 后,我也必须使用 pip install openpyxl 才能使其工作。谢谢!
  • 是的,然后在pd.read_excel()方法中将engine='openpyxl'作为参数传递

标签: python pandas dataframe jupyter xlsx


【解决方案1】:
!pip install xlrd

安装 xlrd 模块

【讨论】:

  • 我收到这条消息:您的 xlrd 版本是 {xlrd_version}。在 xlrd >= 2.0 中,“ ValueError:您的 xlrd 版本是 2.0.1。在 xlrd >= 2.0 中,仅支持 xls 格式。请改为安装 openpyxl。我必须在它之后执行“pip install openpyxl”,现在它可以工作了. 谢谢!
猜你喜欢
  • 2019-07-31
  • 1970-01-01
  • 1970-01-01
  • 2023-01-16
  • 2013-06-29
  • 2019-04-28
  • 1970-01-01
  • 2015-10-25
  • 2018-12-04
相关资源
最近更新 更多