Python dataframe返回所有搜索函数的键错误
下面是我的python代码,它不起作用:Python dataframe返回所有搜索函数的键错误,python,pandas,dataframe,openpyxl,Python,Pandas,Dataframe,Openpyxl,下面是我的python代码,它不起作用: import os, fnmatch from openpyxl import Workbook as wb import openpyxl as opxl import pandas as pd def read_excel_sheets(xls_path): """Read all sheets of an Excel workbook and return a single DataFrame (or in
import os, fnmatch
from openpyxl import Workbook as wb
import openpyxl as opxl
import pandas as pd
def read_excel_sheets(xls_path):
"""Read all sheets of an Excel workbook and return a single DataFrame (or in my case dictionary)"""
print(f'Loading {xls_path} into pandas')
xl = pd.ExcelFile(xls_path)
df = pd.DataFrame()
columns = None
for idx, shtnm in enumerate(xl.sheet_names):
print(f'Reading sheet #{idx}: {shtnm}')
sheet = xl.parse(shtnm)
print("You are seeing the function read_excel_sheets executing.The current sheet name is:\n",shtnm,"\n",type(shtnm))
print("You are seeing the function read_excel_sheets executing.\n",sheet,"\n",type(sheet))
if shtnm == 'Cover Page':
myloc=sheet[1].str.contains('Location').any() #this line does not work
print("first column of sheet\n",sheet[1])
return True
我确实有代码进入名为“封面”的特定电子表格中,但似乎没有任何搜索功能正常工作(或至少返回所需的结果)。我不断地得到这个错误,因为我指出了这条线。返回值被设置为True,以保持这里的简单性。我尝试过find()、findall()、contains()、search(),但它们似乎都与pandas系列相关(或与之相关),而不一定与数据帧相关
return self._engine.get_loc(key)
File "pandas/_libs/index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "read_all_folders_files.py", line 66, in <module>
xlparseop=read_excel_sheets(filename)
File "read_all_folders_files.py", line 39, in read_excel_sheets
print("first column of sheet\n",sheet[1])
File "/Users//anaconda3/lib/python3.8/site-packages/pandas/core/frame.py", line 2800, in __getitem__
indexer = self.columns.get_loc(key)
File "/Users//anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 2648, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/_libs/index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 1
return self.\u引擎。获取位置(键)
文件“pandas/_libs/index.pyx”,第111行,在pandas._libs.index.IndexEngine.get_loc中
文件“pandas/_libs/index.pyx”,第138行,在pandas._libs.index.IndexEngine.get_loc中
文件“pandas/_libs/hashtable_class_helper.pxi”,第1619行,在pandas._libs.hashtable.PyObjectHashTable.get_项中
文件“pandas/_libs/hashtable_class_helper.pxi”,第1627行,在pandas._libs.hashtable.PyObjectHashTable.get_项中
关键错误:1
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“read\u all\u folders\u files.py”,第66行,在
xlparseop=读取excel表格(文件名)
文件“read\u all\u folders\u files.py”,第39行,在read\u excel\u工作表中
打印(“工作表的第一列\n”,工作表[1])
文件“/Users//anaconda3/lib/python3.8/site packages/pandas/core/frame.py”,第2800行,在__
indexer=self.columns.get_loc(键)
文件“/Users//anaconda3/lib/python3.8/site packages/pandas/core/index/base.py”,第2648行,在get_loc中
返回self.\u引擎。获取\u loc(self.\u可能\u cast\u索引器(键))
文件“pandas/_libs/index.pyx”,第111行,在pandas._libs.index.IndexEngine.get_loc中
文件“pandas/_libs/index.pyx”,第138行,在pandas._libs.index.IndexEngine.get_loc中
文件“pandas/_libs/hashtable_class_helper.pxi”,第1619行,在pandas._libs.hashtable.PyObjectHashTable.get_项中
文件“pandas/_libs/hashtable_class_helper.pxi”,第1627行,在pandas._libs.hashtable.PyObjectHashTable.get_项中
关键错误:1
在声明sheet[1]
pandasparse()
返回:parsed:DataFrame或Dict of DataFrames DataFrame时,很可能会发生KeyError。有关何时返回数据帧Dict的更多信息,请参见sheetname参数中的注释。。。。在这种情况下,您可能会调用1作为类似字典的对象的键…打印出type(sheet)
,以查看并确保您正在尝试收集的对象中的值sheet是一个数据帧。是的,因此发生了错误,因为sheet[1]
无效,因为键1
无效。这就是为什么会出现KeyError:1
。找出您需要的有效密钥,您就完成了。尝试print(sheet.\uu dict\uuu)
,您应该会看到属性。