简体   繁体   English

使用python中的pandas将数据写入现有Excel

[英]Writing Data to Existing Excel using pandas in python

Currently, I am exporting two data frames to a single excel file. 目前,我正在将两个数据帧导出到单个excel文件中。 Each data frame is placed on separate sheets. 每个数据框放置在单独的页面上。 I am currently running a simulation and need 2,000 columns of each data frame (2,000 on each sheet). 我目前正在运行模拟,每个数据框需要2,000列(每张纸上需要2,000列)。 Every time I run my code, it creates a new excel file (what I originally intended). 每次我运行代码时,它都会创建一个新的excel文件(本来是我想要的)。 But to save time, I was wondering if it would be possible to write onto an existing excel file by adding new columns of data without writing over the existing data? 但是为了节省时间,我想知道是否可以通过添加新的数据列而不覆盖现有数据来写入现有的excel文件?

######### This is my original code ################
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
from pandas import DataFrame
from openpyxl import Workbook

df1 = pd.DataFrame(returns1)
df2 = pd.DataFrame(returns2)


x1 = 'mypath/ '
x2 = datetime.datetime.now().strftime('%B %d, %Y %H-%M-%S')
x3 = '.xlsx'
destination = x1 + x2 + x3
writer = pd.ExcelWriter(destination, engine='xlsxwriter')

df1.to_excel(writer, sheet_name= 'Returns 1', index=False)
df2.to_excel(writer, sheet_name= 'Returns 2', index=False)
writer.save()
### Update ### 更新

Code works! 代码有效! Thank you for everyone that helped, especially @zhqiat. 谢谢大家的帮助,尤其是@zhqiat。 Below is the final code. 下面是最终代码。 Does exactly what I wanted. 正是我想要的。 I hope this will help others that run into the same issue I did. 我希望这会帮助遇到我同样问题的其他人。

 df1 = pd.DataFrame(returns1) df2 = pd.DataFrame(returns2) book = load_workbook('mypath.xlsx') writer = pd.ExcelWriter('mypath.xlsx', engine='openpyxl') writer.book = book writer.sheets = {ws.title: ws for ws in book.worksheets} df1.to_excel(writer, sheet_name='Returns1', startrow=0, startcol=writer.sheets['Returns1'].max_column, index=False) df2.to_excel(writer, sheet_name='Returns2', startrow=0, startcol=writer.sheets['Returns2'].max_column, index=False) This is what I wanted. A 100 120 119 225 Second Time AB 100 98 120 100 119 105 125 111 Third Time ABC 100 98 106 120 100 99 119 105 101 125 111 89 

and so on... 等等...

Unfortunately appending to excel isn't a fully fledged feature in pandas. 不幸的是,追加到excel并不是熊猫的完整功能。

In your case for a hacked together solution, you can use an excel writer object to stitch the sheets together. 对于被黑客入侵的解决方案,您可以使用excel writer对象将工作表缝合在一起。

It sounds like you want to add columns for all new data (not row) so you would likely need to determine the width of your spreadsheet with 听起来您想为所有新数据添加列(而不是行),所以您可能需要使用以下方法确定电子表格的宽度:

maxcol = writer.sheets['SheetName'].max_column

** Edit, my bad, it is max_column not max_col ** **编辑,我不好,它是max_column而不是max_col **

Refer to a prior question from 2017 for the full code on how to add rows. 有关如何添加行的完整代码,请参阅2017年先前问题

read the excel file and store it as a dataframe and append the series and write to the same excel sheet 读取Excel文件并将其存储为数据框,然后附加系列并写入相同的Excel工作表

# read wherever you have stored the file

prev_df = pd.read_excel('path to file.xlsx')

# convert new series to df
new_df =  pd.DataFrame(var1)

# join
df_to_write = prev_df.join(new_df)

# write to excel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM