【发布时间】: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