pandas對Excel文件的讀寫操做

一、將Excel數據讀爲dataframe

1.1 直接讀取

df = pd.read_excel('data.xlsx')

1.2 根據sheet索引

xls = pd.ExcelFile('data_nb0.xlsx')
df = xls.parse('sheet1')

二、將dataframe寫入表格

writer = pd.ExcelWriter('data.xlsx')
new_df.to_excel(writer, sheet_name='sheet1', index=True)
writer.save()

注意:寫入完畢後要加上writer.save(),不然會報錯:ui

Exception: Exception caught in workbook destructor. Explicit close() may be required for workbook.
相關文章
相關標籤/搜索