英文:
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
- how to get it directly to the body of Email or I have to go via Excel?
- 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
- add thousand separator without adding the unnecessary .0 to the "TCE value" column, not sure why it has .0 now
- 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。以下是文档链接:
- https://xlsxwriter.readthedocs.io/example_conditional_format.html
- https://xlsxwriter.readthedocs.io/worksheet.html
您的用例的示例用法如下。您可以使用上述链接自定义格式:
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:
- https://xlsxwriter.readthedocs.io/example_conditional_format.html
- https://xlsxwriter.readthedocs.io/worksheet.html
A sample usage for your use case is as follows. You can use above links to customize the formattings:
import xlsxwriter
with pd.ExcelWriter("report.xlsx", mode="w", engine="xlsxwriter") as writer:
# Format "TCE value" as int
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"]
# 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({'num_format': '#,###'})
# Format for red text
fmt_red = wb.add_format({'font_color': '#9C0006'})
# Format for green text
fmt_green = wb.add_format({'font_color': '#006100'})
##### Format columns as required #####
# Set thousand separator for "TCE value"
col_idx = df.columns.get_loc("TCE value")
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("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})
Output is truncated due to sensitive nature of data as requested.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论