TypeError:to_excel()为参数'sheet_name'获取了多个值

时间:2017-03-23 10:41:04

标签: 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')
    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'

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

您可以尝试使用:

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

答案 1 :(得分:2)

我测试了一些样本数据,它按预期工作:

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())

输出:

enter image description here

版本:

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