I have an Excel file with two sheets and I am trying to read both of them into a dataframe as in the code below. However, I get the error
KeyError: "['months_to_maturity' 'asset_id' 'orig_iss_dt' 'maturity_dt' 'pay_freq_cd'\n 'coupon' 'closing_price'] not in index"
in the line
return df[['months_to_maturity', 'asset_id', 'orig_iss_dt', 'maturity_dt' , 'pay_freq_cd', 'coupon', 'closing_price']]
in the SecondExcelFileReader() function. However, both sheets have the headers
asset_id orig_iss_dt maturity_dt pay_freq_cd coupon closing_price months_to_maturity
I return df as follows as that is the order in which I want the columns.
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])
Edit: This Excel file was exported from Sybase Oracle SQL Developer and hence the first sheet came already with the titles. I just copied and pasted the second sheet with the same titles. Also, I am only having the issue with the second sheet.


dfbefore selecting a subset of the columns. If it looks good, try to select each column individually and see if you get a KeyError. If so, it could be something silly like extra whitespace in one of the column names.pandas.read_excel? This method includes a number of arguments to control the parsing including asheetnameargument.