python to_excel保存成xls_pd.ExcelWriter(to_excel)保存结果到已存在的excel文件中
網易云課堂該課程鏈接地址
僅存單個excel表格就用df.to_excel更簡單
pd.ExcelWriter保存結果到已存在的excel文件中,并支持多個sheet表格寫入excel。
Python對excel更多功能編程需要安裝openpyxl第三方包
Python代碼如下
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 30 13:24:28 2019
@author: 231469242@qq.com
"""
import numpy as np
import pandas as pd
df1 = pd.DataFrame({"name": ['Alfred', 'Batman', 'Catwoman'],
"toy": [np.nan, 'Batmobile', 'Bullwhip'],
"born": [pd.NaT, pd.Timestamp("1940-04-25"),
pd.NaT]})
df2 = pd.DataFrame({"name": ['Alfred', 'Jim', 'Catwoman'],
"toy": [np.nan, 'Batmobile', 'Bullwhip'],
"born": ['Greens',pd.Timestamp("1940-04-25"),
pd.NaT]})
with pd.ExcelWriter('result.xls') as writer:
df1.to_excel(writer, sheet_name='sheet1')
df2.to_excel(writer, sheet_name='sheet2')
#僅存單個excel表格就用to_excel更簡單
df1.to_excel("result.xlsx")
微信掃二維碼,免費學習更多python資源
總結
以上是生活随笔為你收集整理的python to_excel保存成xls_pd.ExcelWriter(to_excel)保存结果到已存在的excel文件中的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: conda 指定版本python_con
- 下一篇: python语言表白语句简单_简短的表白