from dataframe to the body of Email automatically,several formatting issues: thousand separator, color(red for negative number and green for positive)

huangapple go评论79阅读模式
英文:

from dataframe to the body of Email automatically,several formatting issues: thousand separator, color(red for negative number and green for positive)

问题

以下是翻译好的部分:

我有一个数据框,看起来像这样:

                     日期          值       值 2    每日值变化
    shortCode                                                       
    TD1        2023-01-06       38.67    15162.0               -1.00
    TD2        2023-01-06       53.42    33952.0               -0.40
    TD3C       2023-01-06       52.91    30486.0               -0.36
    TD6        2023-01-06      169.61    90824.0               -3.83
    TD7        2023-01-06      168.56    66685.0               -1.25
    TD8        2023-01-06      244.29    71413.0               -2.42
    TD9        2023-01-06      129.38    24498.0               -2.50
    TD14       2023-01-06      251.19    81252.0               -0.81
    TD15       2023-01-06       54.03    32382.0               -0.56
    TD18       2023-01-06      425.08    71615.0               -2.42

我希望将其作为电子邮件的正文发送,使用Outlook,将来自动化将是很好的(作为每日报告,无需人工干预),但目前我只是在尝试实现一些格式设置

1. 如何直接将其放入电子邮件正文中,还是必须通过Excel?
2. 当通过Excel时,如何确保所有列标题都正确显示?它们部分隐藏了,必须手动点击才能显示完整标题
3. 添加千位分隔符,而不会在“TCE value”列中添加不必要的“.0”,不确定为什么现在有“.0”
4. 在“每日值变化”等列中(由于大小问题未显示所有列),正数显示绿色,负数显示红色。

我所做的:
对于千位分隔符

    df_bdti_final[['值', 'TCE值',
            ]] = df_bdti_final[['值', 'TCE值']].iloc[:, :].applymap('{:,}'.format)

请注意,这只是对您问题的部分回答,我会根据您的要求提供翻译。

英文:

I have a dataframe look like this

                 date       value    value 2  daily value change
shortCode                                                       
TD1        2023-01-06       38.67    15162.0               -1.00
TD2        2023-01-06       53.42    33952.0               -0.40
TD3C       2023-01-06       52.91    30486.0               -0.36
TD6        2023-01-06      169.61    90824.0               -3.83
TD7        2023-01-06      168.56    66685.0               -1.25
TD8        2023-01-06      244.29    71413.0               -2.42
TD9        2023-01-06      129.38    24498.0               -2.50
TD14       2023-01-06      251.19    81252.0               -0.81
TD15       2023-01-06       54.03    32382.0               -0.56
TD18       2023-01-06      425.08    71615.0               -2.42

I wish to send it as the BODY of the Email with Outlook, it would be great to automate it in the future (as daily report without human intervention) but for the moment I just struggle to achieve some formatting

  1. how to get it directly to the body of Email or I have to go via Excel?
  2. to have all the column headers shown properly, when go through Excel they are partly hidden and have to click manually to show the full title
  3. add thousand separator without adding the unnecessary .0 to the "TCE value" column, not sure why it has .0 now
  4. in the columns like "daily value change"(I have a few more columns not shown due to size),
    having green color for positive numbers and red for negatives.

what I did:
for thousand separator

df_bdti_final[['value', 'TCE value',
        ]] = df_bdti_final[['value', 'TCE value']].iloc[:, :].applymap('{:,}'.format)

答案1

得分: 1

您可以使用像xlsxwriter这样的Excel写入引擎以及其格式化API。以下是文档链接:

您的用例的示例用法如下。您可以使用上述链接自定义格式:

import xlsxwriter

with pd.ExcelWriter("report.xlsx", mode="w", engine="xlsxwriter") as writer:
    # 格式化 "TCE value" 为整数
    df["TCE value"] = pd.to_numeric(df["TCE value"], errors='coerce').fillna(0).astype('Int64')

    df.to_excel(writer, sheet_name="report", index=False)
    wb  = writer.book
    ws = writer.sheets["report"]

    # 自动调整列宽
    for c in df:
        max_width = max(df[c].astype(str).map(len).max(), len(c))
        col_idx = df.columns.get_loc(c)
        ws.set_column(col_idx, col_idx, max_width)

    # 用千位分隔符格式化整数
    fmt_int_with_th_sep = wb.add_format({'num_format': '#,###'})

    # 用红色文本格式化
    fmt_red = wb.add_format({'font_color': '#9C0006'})

    # 用绿色文本格式化
    fmt_green = wb.add_format({'font_color': '#006100'})

    ##### 格式化所需的列 #####

    # 为 "TCE value" 设置千位分隔符
    col_idx = df.columns.get_loc("TCE value")
    ws.set_column(col_idx, col_idx, None, fmt_int_with_th_sep)

    # 为负值和正值设置红色和绿色
    col_idx = df.columns.get_loc("daily value change")
    ws.conditional_format(1, col_idx, len(df) + 1, col_idx, {'type': 'cell', 'criteria': '<', 'value': 0, 'format': fmt_red})
    ws.conditional_format(1, col_idx, len(df) + 1, col_idx, {'type': 'cell', 'criteria': '>', 'value': 0, 'format': fmt_green})

由于数据的敏感性要求,输出已被截断。

英文:

You can use some excel writer engine like xlsxwriter and its formatting APIs. The documentation links:

A sample usage for your use case is as follows. You can use above links to customize the formattings:

import xlsxwriter

with pd.ExcelWriter(&quot;report.xlsx&quot;, mode=&quot;w&quot;, engine=&quot;xlsxwriter&quot;) as writer:
    # Format &quot;TCE value&quot; as int
    df[&quot;TCE value&quot;] = pd.to_numeric(df[&quot;TCE value&quot;], errors=&#39;coerce&#39;).fillna(0).astype(&#39;Int64&#39;)

    df.to_excel(writer, sheet_name=&quot;report&quot;, index=False)
    wb  = writer.book
    ws = writer.sheets[&quot;report&quot;]

    # Auto-fit width
    for c in df:
        max_width = max(df[c].astype(str).map(len).max(), len(c))
        col_idx = df.columns.get_loc(c)
        ws.set_column(col_idx, col_idx, max_width)

    # Format for integers with thousand separator
    fmt_int_with_th_sep = wb.add_format({&#39;num_format&#39;: &#39;#,###&#39;})

    # Format for red text
    fmt_red = wb.add_format({&#39;font_color&#39;: &#39;#9C0006&#39;})

    # Format for green text
    fmt_green = wb.add_format({&#39;font_color&#39;: &#39;#006100&#39;})

    ##### Format columns as required #####

    # Set thousand separator for &quot;TCE value&quot;
    col_idx = df.columns.get_loc(&quot;TCE value&quot;)
    ws.set_column(col_idx, col_idx, None, fmt_int_with_th_sep)

    # Set reg and green for negative and positive values
    col_idx = df.columns.get_loc(&quot;daily value change&quot;)
    ws.conditional_format(1, col_idx, len(df) + 1, col_idx, {&#39;type&#39;: &#39;cell&#39;, &#39;criteria&#39;: &#39;&lt;&#39;, &#39;value&#39;: 0, &#39;format&#39;: fmt_red})
    ws.conditional_format(1, col_idx, len(df) + 1, col_idx, {&#39;type&#39;: &#39;cell&#39;, &#39;criteria&#39;: &#39;&gt;&#39;, &#39;value&#39;: 0, &#39;format&#39;: fmt_green})

Output is truncated due to sensitive nature of data as requested.

huangapple
  • 本文由 发表于 2023年1月9日 12:34:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定