我需要用不同的参数使用相同的存储过程。
所以我想遍历参数,并在单独的数据帧中写入存储过程的结果。(数据帧列表)。然后我应该能够使用索引调用每个数据帧
df[0]
或
df[1]
据我所知
df_rpt
是一个数据帧列表,我应该能够通过指定索引调用其中任何一个。
我错了吗?
Python代码:
params = [par1, par2]
# iterate thru each parameter
for par in params:
query = f""" SET NOCOUNT ON; EXEC {stor_proc} {par} """
cursor = connection_string.cursor()
cursor.execute(query)
cursor.nextset() # first returned table from stor proc is irrelevant - so skip it, move on to second one
column_names = [col[0] for col in cursor.description]
df = []
for row in cursor.fetchall():
df.append({name: row[i] for i, name in enumerate(column_names)})
df_rpt = pd.DataFrame(df)
print(df_rpt[0])
错误消息:
File "C:\Users\Username
Files\Python\venvname\.venv\lib\site-packages\pandas\core\frame.py", line 2800, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Users\username
Files\Python\venvname\.venv\lib\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: 0