【发布时间】:2019-10-19 14:27:04
【问题描述】:
我有一个名为 data 的列表,其中包含多个数据框。 data[0]、data[1] 等将显示数据框 1、数据框 2 等。所有数据框的行数和属性数都不同。我想知道有没有一种方法可以编写 data[1][2] 来提取数据框 2 和列 3 名称。即 data[i][j] 表示第 (i+1) 个数据帧和第 (i+1) 个数据帧中的第 (j+1) 个属性。
列表(数据[1].columns.values) 我知道上面的代码给了我所有的属性。我有兴趣了解使用上述方式的索引 - data[i][j]。
import os
import pandas as pd
path = os.getcwd()
files = os.listdir(path)
files
files_xlxs = [f for f in files if f[-4:] == 'xlsx']
files_xlxs = [f for f in files_xlxs if '$' not in f]
data = []
for f in files_xlxs:
pathFile = path + '\\' + f
print(pathFile)
data.append(pd.read_excel(pathFile))
data[1]
Data[1][2] 应该从数据框 2 中提取第三列名称。编译后出现以下错误。
KeyError Traceback(最近一次调用最后一次) D:\Anaconda\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 2656 尝试: -> 2657 返回 self._engine.get_loc(key) 2658 除了 KeyError:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
密钥错误:2
在处理上述异常的过程中,又发生了一个异常:
KeyError Traceback(最近一次调用最后一次) 在 () ----> 1 个数据[1][2]
【问题讨论】:
标签: pandas list dataframe indexing jupyter