在行和值使用相同字段时出现问题

问题描述

我正在用python创建数据透视表,但其中一个表遇到了麻烦。关于此表的独特之处在于,我在“行”和“值”中都使用了相同的字段(获取计数)。我可以在excel中做到这一点,但似乎在python中进行设置时,为Values设置字段将覆盖为Rows设置。

我使用this作为指导,这是相关的代码

# Sets the rows,columns and filters of the pivot table
print(f'Setting rows,columns,and filters of the pivot table ({pt_name}) for {ws1.Name}')
for field_list,field_r in ((pt_filters,win32c.xlPageField),(pt_rows,win32c.xlRowField),(pt_cols,win32c.xlColumnField)):
    for i,value in enumerate(field_list):
        pt_ws.Pivottables(pt_name).PivotFields(value).Orientation = field_r
        pt_ws.Pivottables(pt_name).PivotFields(value).Position = i + 1

# Sets the Values of the pivot table
print(f'Setting the values of the pivot table ({pt_name}) for {ws1.Name}')
    for field in pt_fields:
        pt_ws.Pivottables(pt_name).Adddatafield(pt_ws.Pivottables(pt_name).PivotFields(field[0]),field[1],field[2]).NumberFormat = field[3]

对于这个有问题的数据透视表,我将其传递给行和值:

pt_rows = ['Field_X']
pt_fields = [['Field_X','Count of Field_X',win32c.xlCount,'0']]

,最终结果是一个this的表-请注意,它只是过滤器和计数,并且缺少行。如果我检查表的字段列表面板,它在行中什么也不显示,但是我可以毫无问题地将其添加到excel中,因此看起来像should

那么,如何在行和值中使用相同的字段而不覆盖其他字段?

更新:我尝试交换顺序(在行前设置“值”)并修复了该表,但在“值和列使用相同字段”的其他表中创建了问题。因此,肯定要在Values中使用与其他字段相同的字段...

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)