【问题标题】:Xlsxwriter conditional formatting in loop problem循环问题中的 Xlsxwriter 条件格式
【发布时间】:2021-02-24 11:37:52
【问题描述】:

我有带有多头的 Pandas 数据帧,我只在循环中的几列上应用条件格式(例如,6、7、9、10、12、13...)我已经用于循环,如下所示:

    for i in range(6,len(df1.columns),3):
            print(i)
            worksheet.conditional_format(4,int(i),len(df1)+3,int(i+1), {'type':     'cell', \
                                            'criteria': '>=', \
                                            'value':'45',\
                                            'format':   format1
                                        })

这会使我的 Excel 输出损坏,而且如果我修复 Excel,我可以看到我的标题移动了 1 个单元格。此外,该 Excel 中没有进行条件格式设置。任何人都可以帮助解决这个问题吗? 此外,如果我从代码输出中删除这一行很好,但没有条件格式。

这里是示例代码:

import pandas as pd
th='30000'
cars = {'day':['aug','aug','sep','sep','aug'],
        'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4','Hyundai Elite i20'],
        'Type':['sedan,','sedan','hatchback','hatchback','hatchback'],
        'Down Price': [22000,25000,27000,35000,10000]
        }

df = pd.DataFrame(cars, columns = ['day','Brand', 'Type','Down Price'])
dfpivot=pd.pivot_table(df,index=['day'],columns=['Brand','Type'],values=['Down Price'],aggfunc=np.max)
with pd.ExcelWriter(OUTPATH+'Report.xlsx', engine='xlsxwriter') as writer:

    dfpivot.to_excel(writer,sheet_name = 'data')
    workbook = writer.book
    worksheet = writer.sheets['data']
    format1 = workbook.add_format({'bg_color': '#FFC7CE',
                               'font_color': '#9C0006'})
    for i in range(1,len(dfpivot.columns),1):
            print(i)
            worksheet.conditional_format(4,int(i),len(df1)+3,int(i+1), {'type':     'cell', \
                                            'criteria': '>=', \
                                            'value':th,\
                                            'format':   format1
                                    })
writer.save()

【问题讨论】:

标签: pandas conditional-formatting xlsxwriter


【解决方案1】:

我重新格式化了您的代码并修复了一些小问题:

import pandas as pd
import numpy as np

threshold = '30000'
cars = {'day': ['aug', 'aug', 'sep', 'sep', 'aug'],
        'Brand': ['Honda Civic', 'Toyota Corolla', 'Ford Focus',
                  'Audi A4', 'Hyundai Elite i20'],
        'Type': ['sedan,', 'sedan', 'hatchback', 'hatchback', 'hatchback'],
        'Down Price': [22000, 25000, 27000, 35000, 10000]}

df = pd.DataFrame(cars, columns=['day', 'Brand', 'Type', 'Down Price'])
dfpivot = pd.pivot_table(df, index=['day'],
                         columns=['Brand', 'Type'],
                         values=['Down Price'], aggfunc=np.max)

with pd.ExcelWriter('Report.xlsx', engine='xlsxwriter') as writer:

    dfpivot.to_excel(writer, sheet_name='data')
    workbook = writer.book
    worksheet = writer.sheets['data']
    format1 = workbook.add_format({'bg_color': '#FFC7CE',
                                   'font_color': '#9C0006'})
    max_column = len(cars['Brand'])
    worksheet.set_column(1, max_column, 15)

    for i in range(1, len(dfpivot.columns), 1):
            print(i)
            worksheet.conditional_format(4, i, len(df) + 3, i + 1,
                                         {'type': 'cell',
                                          'criteria': '>=',
                                          'value': threshold,
                                          'format': format1})

它运行并且输出文件具有条件格式:

所以,XlsxWriter 代码有效。但是,这些范围看起来不正确(它们是重叠的),因此您应该检查是否使用了正确的行号和列号。

此外,可能不需要单独的条件格式,因为条件在所有情况下都是相同的。在循环之外创建一种条件格式可能就足够了。

【讨论】:

  • 感谢您的时间和回答,它有帮助。我已经更正了范围并且它起作用了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-06
  • 2018-09-09
  • 2021-10-19
  • 1970-01-01
相关资源
最近更新 更多