python - 无法在 Pandas 中读取 Excel 文件

标签 python pandas jupyter-notebook

我正在使用从包含笔记本和 Excel 文件的同一目录启动的 Juypter。我使用以下命令:

import pandas as pd
!ls      # Returns the contents of the working directory.  Use "dir" in a Windows.

返回:

书1.xlsx
common_excel_tasks.ipynb

然后我使用以下命令:
excel_file = 'Book1.xlsx'
df1 = pd.read_excel(excel_file)

但我收到以下错误:
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-3-28585effb1ed> in <module>
      1 excel_file = 'Book1.xlsx'
----> 2 df1 = pd.read_excel(excel_file)

/usr/local/lib/python3.7/site-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs)
    206                 else:
    207                     kwargs[new_arg_name] = new_arg_value
--> 208             return func(*args, **kwargs)
    209 
    210         return wrapper

/usr/local/lib/python3.7/site-packages/pandas/io/excel/_base.py 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, verbose, parse_dates, date_parser, thousands, comment, skip_footer, skipfooter, convert_float, mangle_dupe_cols, **kwds)
    308 
    309     if not isinstance(io, ExcelFile):
--> 310         io = ExcelFile(io, engine=engine)
    311     elif engine and engine != io.engine:
    312         raise ValueError(

/usr/local/lib/python3.7/site-packages/pandas/io/excel/_base.py in __init__(self, io, engine)
    817         self._io = _stringify_path(io)
    818 
--> 819         self._reader = self._engines[engine](self._io)
    820 
    821     def __fspath__(self):

/usr/local/lib/python3.7/site-packages/pandas/io/excel/_xlrd.py in __init__(self, filepath_or_buffer)
     18         """
     19         err_msg = "Install xlrd >= 1.0.0 for Excel support"
---> 20         import_optional_dependency("xlrd", extra=err_msg)
     21         super().__init__(filepath_or_buffer)
     22 

/usr/local/lib/python3.7/site-packages/pandas/compat/_optional.py in import_optional_dependency(name, extra, raise_on_missing, on_version)
     91     except ImportError:
     92         if raise_on_missing:
---> 93             raise ImportError(message.format(name=name, extra=extra)) from None
     94         else:
     95             return None

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

最佳答案

您应该安装 xlrd依赖性。
pip install xlrd应该为你做的伎俩。

希望这可以帮助!

关于python - 无法在 Pandas 中读取 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58368591/

相关文章:

python - 在python中使用bs4从网站的不同链接获取律师详细信息

python-3.x - 如何使用 Pandas 更改 Excel 中第一行的颜色

python - IPython (Jupyter) 笔记本中的交互式调试

python - 如何在绘图中为一个 x 值绘制具有多个 y 值的折线图?

python - 来自 Google Finance 的网络抓取 : returned data list always empty

Python KNeighbors 分类器

python - 如何在django中的过滤器类中设置后端过滤器范围

python - Kafka生产者不选择新分区

python - 在具有缺失值的列上计算 sin

jupyter-notebook - 如何将 Jupyter Notebook 转换为学术论文格式?