简体   繁体   English

在现有的 Excel 工作表上保存 Pandas 系列

[英]Saving Pandas series on existing excel worksheet

I am trying to export some values from python to excel.我正在尝试将一些值从 python 导出到 excel。 In the first part I want to setup the sheet and save it in a xlsx file.在第一部分中,我想设置工作表并将其保存在 xlsx 文件中。 After that I want to overwrite the same worksheet by adding a pandas series.之后,我想通过添加一个熊猫系列来覆盖同一个工作表。

My issue comes from the fact that when I run my code, I obtain a brand new worksheet with the name 'Name1' instead instead of putting the Series on the existing sheet named "Name" previously.我的问题来自这样一个事实,即当我运行我的代码时,我获得了一个名为“Name1”的全新工作表,而不是将系列放在之前名为“Name”的现有工作表上。

import pandas as pd
from openpyxl import Workbook
from openpyxl import load_workbook

#Page setup
wb = Workbook()
ws0 = wb.active
ws0.title = "Name"
ws0.sheet_properties.tabColor = "1072BA"
ws0['B1'] = "AAA"
wb.save('Example.xlsx')
wb.close()

#Insert pandas series on existing worksheet
path = r".\Example.xlsx"
book = load_workbook('Example.xlsx')
writer = pd.ExcelWriter(path, engine = 'openpyxl')
writer.book = book

r1y = pd.Series([1,3,5,13,6,8])
df1y = pd.DataFrame(r1y)

df1y.to_excel(writer, 'Name', header=False, startcol = 0, startrow = 1)
writer.save()
writer.close()

What am I doing wrong?我究竟做错了什么?

Thanks in advance提前致谢

Obviously to_excel creates a new worksheet and does not want to write in an already created one.显然to_excel创建了一个新的工作表并且不想写入已经创建的工作表。 You are already having one, which is called Name , thus the new one is Name1 .您已经有了一个,名为Name ,因此新的为Name1 If you do not have a sheet MyOwnName this will crete one and write the data there:如果您没有MyOwnName表,这将生成一个并在那里写入数据:

df1y.to_excel(writer, 'MyOwnName', header=False, startcol = 0, startrow = 1)

Although I could not find anything mentioning it in the documentation: https://pandas.pydata.org/pandas- docs/stable/generated/pandas.DataFrame.to_excel.html虽然我在文档中找不到任何提及它的内容: https : //pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_excel.html

I have found the answer to this problem in a different website, which I report here.我在另一个网站上找到了这个问题的答案,我在这里报告。 I have not come up with this answer myself, so I want to credit who did: https://cmsdk.com/python/insert-pandas-dataframe-into-existing-excel-worksheet-with-styling.html我自己还没有想出这个答案,所以我想感谢谁做了: https : //cmsdk.com/python/insert-pandas-dataframe-into-existing-excel-worksheet-with-styling.html

from openpyxl import load_workbook, Workbook
import pandas as pd
df = pd.DataFrame(data=["20-01-2018",4,9,16,25,36])
path = 'filepath.xlsx'
writer = pd.ExcelWriter(path, engine='openpyxl')
writer.book = load_workbook(path)
**writer.sheets = dict((ws.title,ws) for ws in writer.book.worksheets)**
df.to_excel(writer,sheet_name="Sheet1", startrow=2,index=False, header=False)
writer.save()

What did the job for me was the row highlighted in bold.对我有用的是粗体突出显示的行。 I hope it can be of help to you!我希望它能对你有所帮助!

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

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