網易雲課堂該課程鏈接地址
僅存單個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資源