程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How does pandas create a new worksheet on an existing excel table and add a dataframe?

編輯:Python

If we want to create a new Excel file , And will DataFrame Add to Excel The worksheet for is simple .

import pandas as pd
import numpy as np
df1=pd.DataFrame(np.arange(7))
df2=pd.DataFrame(np.arange(7))
writer=pd.ExcelWriter("Excel.xlsx")
df1.to_excel(writer,"first") #first Is the name of the first worksheet 
df2.to_excel(writer,"second") #second Is the name of the second worksheet 
writer.save()

But if one already exists Excel file , If we use the above code, we will overwrite the contents of the original file , That is to delete the original data , The new code looks like this :

import pandas as pd
from openpyxl import load_workbook
book = load_workbook('Excel.xlsx')
writer=pd.ExcelWriter("Excel.xlsx",engine='openpyxl')
writer.book = book
df1.to_excel(writer,"first")
df2.to_excel(writer,"second")
writer.save()

The last attached Github The official answer :pandas How to in the existing Excel New sheet on table ?


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved