python - KeyError when reading from Excel data into dataframe -


i have excel file 2 sheets , trying read both of them dataframe in code below. however, error

keyerror: "['months_to_maturity' 'asset_id' 'orig_iss_dt' 'maturity_dt' 'pay_freq_cd'\n 'coupon' 'closing_price'] not in index" 

in line

return df[['months_to_maturity', 'asset_id', 'orig_iss_dt', 'maturity_dt' , 'pay_freq_cd', 'coupon', 'closing_price']] 

in secondexcelfilereader() function. however, both sheets have headers

asset_id    orig_iss_dt maturity_dt  pay_freq_cd    coupon  closing_price   months_to_maturity 

i return df follows order in want 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: excel file exported sybase oracle sql developer , hence first sheet came titles. copied , pasted second sheet same titles. also, having issue second sheet.

sheet 1: sheet 1

sheet 2: sheet 2

def excelfilereader():     xls = pd.excelfile('d:/usdatarecently.xls')     sheet_num = xls.sheet_names.index(xls.sheet_names[0])     df = pd.read_excel('d:/usdatarecently.xls',sheetname=sheet_num)     return df[['months_to_maturity', 'asset_id', 'orig_iss_dt', 'maturity_dt' ,'pay_freq_cd', 'coupon', 'closing_price']] 

alternatively in case instead of sheetname = xls.sheet_names[0] use sheetname=0

looks issue second sheetname "sheet1" , based on excelparser documentation "sheet1" means first sheet, in case it's second sheet. http://pandas.pydata.org/pandas-docs/stable/generated/pandas.excelfile.parse.html

a better implementation be:

def mergingdataframes():     mergedf= pd.concat(pd.read_excel('d:/usdatarecently.xls', sheetname=[0,1]))     mergedf.index = mergedf.index.droplevel(0)# need drop dict keys     return mergedf 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -