gpt4 book ai didi

python - 使用 writer.sheets 方法时的 Pandas.ExcelWriter KeyError

转载 作者:行者123 更新时间:2023-12-03 23:35:31 25 4
gpt4 key购买 nike

请帮忙,我不知道为什么会发生此错误。我以前使用过这个代码,没有任何问题。我希望这不是愚蠢的事情。总是感谢帮助。

版本:

python 3.6

PD 0.23.0

xlsxwriter 1.0.4

writer = pd.ExcelWriter('Output.xlsx', engine='xlsxwriter')
workbook = writer.book
worksheet = writer.sheets['Sheet1']

输出:
Traceback (most recent call last):
File "/opt/eclipse/dropins/plugins/org.python.pydev.core_7.2.0.201903251948/pysrc/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
exec(exp, global_vars, local_vars)
File "<console>", line 1, in <module>
KeyError: 'Sheet1'

最佳答案

您没有创建 Sheet 1。

来自 here有一个例子:

import pandas as pd

# Create a Pandas dataframe from the data.
df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1') ***#this is where you create Sheet 1***

# Get the xlsxwriter objects from the dataframe writer object.
workbook = writer.book
worksheet = writer.sheets['Sheet1'] ***#here is where you select it***

关于python - 使用 writer.sheets 方法时的 Pandas.ExcelWriter KeyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58937277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com