pandas对Excel文件的读写操作

1、将Excel数据读为dataframe

1.1 直接读取

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

1.2 根据sheet索引

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

2、将dataframe写入表格

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

注意:写入完毕后要加上writer.save(),否则会报错:

Exception: Exception caught in workbook destructor. Explicit close() may be required for workbook.
posted @ 2019-04-01 16:50  秋沐霖  阅读(5584)  评论(0编辑  收藏  举报
Live2D