[英]python: update dataframe to existing excel sheet without overwriting contents on the same sheet and other sheets
[英]How to update the same excel sheet with different dataframe without overwriting the previous data present in excel file using python2.7.14?
可以說我有3個不同的數據框,它們正在循環中生成數據框。 所以目前我正在將數據框轉換為excel文件。 我只能將一個數據幀轉換為excel。 但是我需要用不同的數據更新所有3個數據幀,應在excel文件中一個接一個地追加。 我該如何實現? 有可能嗎?
我當前的代碼。 我將不同的數據框轉換為Excel文件的不同工作表。
注意:我只需要追加到一張Excel工作表 。
for itr in range(0,3):
df["users"] = UserArray
for action in range(len(ColumnName)):
Array = []
for user in range(int(usercount)):
#calling Copylogs function to copy the required logs and actions in script into another file
Array.append(CopyLogs(abs_read_path,itr+1,user+1,ColumnName[action]))
Array1 = AverageStatistics(Array)
df[ColumnName[action]] = Array1
print df
print "\n"
df.to_excel(writer,sheet_name="TimeStampsOfItr="+str(itr+1))
writer.save()
您的意思是將數據幀寫入文件中的不同工作表嗎? 可以使用pandas.DataFrame.to_excel的sheet_name =參數完成此操作,盡管在執行下一個操作之前,必須確保每次寫入均已完成並釋放了文件。 嘗試將writer.save()移入循環。 如果您不想這樣做,請分別建立writer實例,並將其用於不同工作表上的多個數據框。
如果要連續寫入excel文件中的同一張紙,但要在空間上重疊的不同位置寫入數據,則需要將不同的數據以大熊貓的形式組合在一起,然后從單個數據框中寫入目標紙。 例如,熊貓向excel寫入的詳細信息沒有提供僅寫入奇數行然后僅寫入偶數行的機制。
編輯:更多信息添加到問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.