在表格视图/重复项中使用Excel数据透视表

时间:2018-12-31 09:10:10

标签: python-3.x pandas pivot-table

我想将表格中用python / pandas完成的数据透视表发送到excel,在表格视图中每行重复项目值:

谢谢您的帮助!

代码在这里:

df2 = pd.pivot_table(dataframe,index= 
['COD_ARTC','COD_COLR','START_CONF_WEEK', 'START_REQ_WEEK'],values = 
'QTY_ORDERED',aggfunc=np.sum)
writer_orig = pd.ExcelWriter('test2.xlsx', engine='xlsxwriter')
df2.to_excel(writer_orig, index=True)
writer_orig.save()

在文件“ test2.xlsx”中输出:

COD_ARTC COD_COLR START_CONF_WEEK START_REQ_WEEK    QTY_ORDERED
SKU41-51 black  201852            201852              15
        201903            201851              6
                      201903              33

但是我想要这个:

COD_ARTC  COD_COLR START_CONF_WEEK  START_REQ_WEEK  QTY_ORDERED
SKU41-51 black     201852   201852                  15
SKU41-51 black     201903   201851                  6
SKU41-51 black     201903   201903                   33

1 个答案:

答案 0 :(得分:0)

如果我在to_ excel中添加merge_cells = False,它会起作用

df2.to_excel(writer_orig,index = True,merge_cells = False)