为了账号安全,请及时绑定邮箱和手机立即绑定

使用 xlsxwriter 将数据从 Dataframe 写入 Excel 表时出错

使用 xlsxwriter 将数据从 Dataframe 写入 Excel 表时出错

qq_笑_17 2021-09-14 10:30:49
我有一个数据框,我正在尝试使用 xlsxwriter 模块将其写入 Excel 工作表。下面给出的是我的数据框的样子:prod_id,prod_name,price,unitsprod_a,apple,100,10prod_b,mango,10,123prod_c,orange,12,14我正在尝试将上述数据框编写为如下所示:# Pulling data for the sheetrow = 1col = 0# Iterate through the array you have and unpack the tuple at each indexfor elm1, elm2, elm3, elm4 in df:    worksheet.write(row, col, elm1, number_format)    worksheet.write(row, col + 1, elm2, number_format)    worksheet.write(row, col + 2, elm3, number_format)    worksheet.write(row, col + 3, elm4, number_format)    row += 1它抛出一个错误for elm1, elm2, elm3, elm4 in df:ValueError: too many values to unpack (expected 4)
查看完整描述

1 回答

?
Cats萌萌

TA贡献1805条经验 获得超9个赞

错误是因为,您正在循环df使用多个变量。相反,您应该只有一个变量。


如果我理解正确,您可以像这样调整循环:


for row in df.itertuples():

    elm1,elm2,elm3,elm4 = row[1], row[2], row[3], row[4]

    worksheet.write(row, col, elm1, number_format)

    worksheet.write(row, col + 1, elm2, number_format)

    worksheet.write(row, col + 2, elm3, number_format)

    worksheet.write(row, col + 3, elm4, number_format)

    row += 1

这应该有效。


查看完整回答
反对 回复 2021-09-14
  • 1 回答
  • 0 关注
  • 501 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号