Python TypeError:to#excel()为参数';获取了多个值;活页名称';

Python TypeError:to#excel()为参数';获取了多个值;活页名称';,python,python-3.x,pandas,Python,Python 3.x,Pandas,我尝试将不同的数据帧保存到不同的工作表,如下所示: import pandas as pd from pandas import ExcelWriter import xlsxwriter // code bio = BytesIO() with pd.ExcelWriter(bio, engine='xlsxwriter') as writer: dfStats.to_excel(writer, sheet_name='Summary') dfStockdat

我尝试将不同的数据帧保存到不同的工作表,如下所示:

import pandas as pd
from pandas import ExcelWriter
import xlsxwriter

// code
bio         = BytesIO()
with pd.ExcelWriter(bio, engine='xlsxwriter') as writer:
    dfStats.to_excel(writer, sheet_name='Summary')
    dfStockdata.to_excel(writer, sheet_name='HistoricalISIN')

# create the workbook
writer.save() // tried both with and without this line
bio.seek(0)
workbook    = bio.read()
excelFile   = workbook
但是,我收到以下错误:

TypeError: to_excel() got multiple values for argument 'sheet_name'
如何解决此问题?

您可以尝试使用:

writer = pd.ExcelWriter('output//out.xlsx', engine='xlsxwriter')
dfStats.to_excel(writer, sheet_name='Summary')
dfStockdata.to_excel(writer, sheet_name='HistoricalISIN')
writer.save()

我使用一些样本数据进行了测试,结果与预期相符:

import pandas as pd
from io import BytesIO

bio = BytesIO()

df1 = pd.DataFrame({'Data': [11, 12, 13, 14]})
df2 = pd.DataFrame({'Data': [21, 22, 23, 24]})

with pd.ExcelWriter(bio, engine='xlsxwriter') as writer:
    df1.to_excel(writer, sheet_name='Summary')
    df2.to_excel(writer, sheet_name='HistoricalISIN')

with open('pandas_file.xlsx', 'wb') as w:
    w.write(bio.getvalue())
输出:

版本:

Python:     Python 2.7.13 :: Anaconda 4.3.1 (64-bit)
xlsxwriter: 0.9.6
pandas:     0.19.2

如果您的代码在复制+粘贴后运行,那么添加导入:)这可能是因为您使用的是普通的ExcelWriter,而不是pandas。ExcelWriter?
sheet\u name='Summary'
sheet\u name='HistoricalISIN'
第一个值是
filename
?是否需要使用BytesIO?我正在使用pandas ExcelWriter。将更新代码描述。需要使用BytesIO是。请参阅:将Excel文件写入内存:是,但这将在本地保存文件。我试图避免这种情况,因此使用BytesIO.Note保存它。我试着像你说的那样做,但它仍然给我同样的错误。奇怪的是,这个代码(具有不同的dfs和工作表名称)适合我。你用的是哪一个熊猫版本?版本0.19.2和python3I一起使用。dfStockdata由多个“表”组成。所以我删除了sheet_name属性,然后它打印了多张纸,而不是2张