【发布时间】:2015-07-26 01:54:39
【问题描述】:
我有一个带有两张纸的 Excel 文件,我正在尝试将它们都读入数据框中,如下面的代码所示。但是,我收到错误
KeyError: "['months_to_maturity' 'asset_id' 'orig_iss_dt' 'maturity_dt' 'pay_freq_cd'\n 'coupon' 'closing_price'] not in index"
在行中
return df[['months_to_maturity', 'asset_id', 'orig_iss_dt', 'maturity_dt' , 'pay_freq_cd', 'coupon', 'closing_price']]
在SecondExcelFileReader() 函数中。但是,两张纸都有标题
asset_id orig_iss_dt maturity_dt pay_freq_cd coupon closing_price months_to_maturity
我如下返回df,因为这是我想要列的顺序。
def ExcelFileReader():
xls = pd.ExcelFile('D:/USDataRECENTLY.xls')
df = xls.parse(xls.sheet_names[0])
return df[['months_to_maturity', 'asset_id', 'orig_iss_dt', 'maturity_dt' , 'pay_freq_cd', 'coupon', 'closing_price']]
def SecondExcelFileReader():
xls = pd.ExcelFile('D:/USDataRECENTLY.xls')
df = xls.parse(xls.sheet_names[1])
return df[['months_to_maturity', 'asset_id', 'orig_iss_dt', 'maturity_dt' , 'pay_freq_cd', 'coupon', 'closing_price']]
def mergingdataframes():
df1 = ExcelFileReader()
df2 = SecondExcelFileReader()
return pd.concat([df1, df2])
编辑:这个 Excel 文件是从 Sybase Oracle SQL Developer 导出的,因此第一个工作表已经带有标题。我刚刚复制并粘贴了具有相同标题的第二张纸。另外,我只有第二张纸有问题。
【问题讨论】:
-
您没有得到第一张纸的问题?
-
@AnandSKumar 我对第一张纸没有意见。我必须解释一下,这个 Excel 文件是从 Sybase Oracle SQL Developer 导出的,因此第一张表已经带有标题。我只是复制并粘贴了带有标题的第二张纸。
-
你能显示第二张和第一张(可能是截图)吗?
-
首先查看输出
df,然后再选择列的子集。如果看起来不错,请尝试单独选择每一列并查看是否收到 KeyError。如果是这样,它可能是一些愚蠢的东西,比如其中一个列名中的额外空格。 -
@user131983 你为什么要以这种方式阅读这些文件?为什么不使用
pandas.read_excel?此方法包含许多用于控制解析的参数,包括sheetname参数。