pandas将多个表格(series/dataframe)写入单个或多个excel文件,.to_excel()。
如果提示缺少包则需安装openpyxl
1
| conda install -c conda-forge openpyxl
|
单个文件写出
1 2 3 4 5
| df.to_csv("./test.csv", encoding = 'utf-8', index=False) df.to_excel("./test.xlsx", index=False)
wb_count = df['weibo_count'].value_counts() wb_count.to_excel("./test.xlsx",index_label='value')
|
多个文件写出,类似于文本文件的写出
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| save_file = "./valuecount.xlsx" with pd.ExcelWriter(save_file) as writer: df['wb'].to_excel(writer,sheet_name='wb') df['wb17'].value_counts().to_excel(writer, index_label='value',sheet_name='17') df['wb18'].value_counts().to_excel(writer, index_label='value',sheet_name='18') df['wb19'].value_counts().to_excel(writer, index_label='value',sheet_name='19')
save_file = "./valuecount.xlsx" writer = pd.ExcelWriter(save_file) df1.to_excel(writer, sheet_name = '1') df2.to_excel(writer, sheet_name = '2')
writer.close()
|
追加表格到文件(建议先备份避免出错导致原始表格被覆盖)
1 2
| with pd.ExcelWriter('output.xlsx',mode='a') as writer: df.to_excel(writer, sheet_name='Sheet_add')
|
其他可选参数:na_rep(缺失值填充,默认为空)