这是我尝试的代码。我正在尝试阅读excel文件中存在的工作表,因此尝试使用ExcelFile(),但是不确定为什么会出现以下错误。
# Import pandas
import pandas as pd
# Assign spreadsheet filename: file
xls = pd.read_excel('battledeath.xlsx')
# Load spreadsheet: xls
xls = pd.ExcelFile(pd.read_excel('battledeath.xls'))
并看到此错误:
ValueError Traceback (most recent call last)
<ipython-input-1-c639baabe58f> in <module>
7
8 # Load spreadsheet: xls
----> 9 xls = pd.ExcelFile(pd.read_excel('battledeath.xls'))
10
C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\excel.py in __init__(self, io, **kwds)
374 io = _urlopen(self._io)
375 elif not isinstance(self.io, (ExcelFile, xlrd.Book)):
--> 376 io, _, _, _ = get_filepath_or_buffer(self._io)
377
378 if engine == 'xlrd' and isinstance(io, xlrd.Book):
C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\common.py in get_filepath_or_buffer(filepath_or_buffer, encoding, compression, mode)
216 if not is_file_like(filepath_or_buffer):
217 msg = "Invalid file path or buffer object type: {_type}"
--> 218 raise ValueError(msg.format(_type=type(filepath_or_buffer)))
219
220 return filepath_or_buffer, None, compression, False
ValueError: Invalid file path or buffer object type: <class 'pandas.core.frame.DataFrame'>
答案 0 :(得分:0)
我认为您可能会混淆read_excel
和ExcelFile
之间的区别,总而言之,一个是函数,如果有的话,它会为您创建一个ExcelFile
实例需要,另一个是一堂课,但是,老实说,您真的不需要为此担心太多。
总而言之,您所得到的错误恰恰是它在界面上所说的,ExcelFile
令人困惑,为什么要将数据帧传递给它的构造函数。要解决您的问题,只需使用
xls = pd.read_excel('battledeath.xlsx')
现在,xls
是一个包含您的数据的数据框。另外,只需提示,请阅读https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html