使用pandas比较时间,并根据结果更改字体。

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

Compare times with pandas and change font according to result

问题

我需要更改单元格的颜色,如果其值低于08:00:00,则在整数上有效,但在字符串上无效。

import xlsxwriter

workbook = xlsxwriter.Workbook('test.xlsx')
worksheet1 = workbook.add_worksheet()

# 添加格式。浅红色填充与深红色文本。
format1 = workbook.add_format({'bg_color': '#FFC7CE', 'font_color': '#9C0006'})

# 添加格式。绿色填充与深绿色文本。
format2 = workbook.add_format({'bg_color': '#C6EFCE', 'font_color': '#006100'})

import pandas as pd

data = pd.DataFrame({'Time': ['07:02:07', '15:16:55', '15:17:20', '15:28:58', '15:32:28', '15:38:54'],
                     'Payload': ['[0]->[1]', '[1]->[0]', '[0]->[1]', '[0]->[1]', '[1]->[0]', '[0]->[1]']})

caption = ('Cells with values >= 08:00:00 are in light red. '
           'Values < 50 are in light green.')

# 写入数据。
worksheet1.write('A1', caption)

for row, row_data in enumerate(data):
    worksheet1.write_row(row + 2, 1, row_data)

if len(data) > 0:
    for i in range(len(data)):
        worksheet1.write('D' + str(i + 5), (data['Time'].iloc[i]))
        worksheet1.write('E' + str(i + 5), (data['Payload'].iloc[i]))

# 在范围上写入条件格式。
worksheet1.conditional_format('D5:D12', {'type': 'cell',
                                         'criteria': '>=',
                                         'value': '08:00:00',
                                         'format': format1})

workbook.close()

上面的代码只是一个示例,在我的原始代码中,时间是先前计算的结果。

英文:

I need to change the color of a cell if its value is lower than 08:00:00, it worked on integer but not on strings

import xlsxwriter

workbook = xlsxwriter.Workbook(&#39;test.xlsx&#39;)
worksheet1 = workbook.add_worksheet()


# Add a format. Light red fill with dark red text.
format1 = workbook.add_format({&#39;bg_color&#39;: &#39;#FFC7CE&#39;,
                               &#39;font_color&#39;: &#39;#9C0006&#39;})

# Add a format. Green fill with dark green text.
format2 = workbook.add_format({&#39;bg_color&#39;: &#39;#C6EFCE&#39;,
                               &#39;font_color&#39;: &#39;#006100&#39;})


import pandas as pd

data= pd.DataFrame({&#39;Time&#39;:[&#39;07:02:07&#39;, &#39;15:16:55&#39;, &#39;15:17:20&#39;, &#39;15:28:58&#39;,&#39;15:32:28&#39;,&#39;15:38:54&#39;],
 &#39;Payload&#39;:[&#39;[0]-&gt;[1]&#39;, &#39;[1]-&gt;[0]&#39;,&#39;[0]-&gt;[1]&#39;,&#39;[0]-&gt;[1]&#39;,&#39;[1]-&gt;[0]&#39;,&#39;[0]-&gt;[1]&#39;]})

caption = (&#39;Cells with values &gt;= 08:00:00 are in light red. &#39;
           &#39;Values &lt; 50 are in light green.&#39;)

# Write the data.
worksheet1.write(&#39;A1&#39;, caption)

for row, row_data in enumerate(data):
    worksheet1.write_row(row + 2, 1, row_data)

if len (data) &gt;0:    
    for i in range (len(data)):

        worksheet1.write(&#39;D&#39;+str(i+5),(data[&#39;Time&#39;].iloc[i]))
        worksheet1.write(&#39;E&#39;+str(i+5),(data[&#39;Payload&#39;].iloc[i]))

        


# Write a conditional format over a range.
worksheet1.conditional_format(&#39;D5:D12&#39;, {&#39;type&#39;: &#39;cell&#39;,
                                         &#39;criteria&#39;: &#39;&gt;=&#39;,
                                         &#39;value&#39;: &#39;08:00:00&#39;,
                                         &#39;format&#39;: format1})

workbook.close()

the code above is just an example, in my original code the time is the result of previous calculations.

答案1

得分: 2

以下是翻译好的内容:

在代码中存在一些问题,但主要问题是像'07:02:07'这样的值是字符串,而不是日期/时间。

为了使它们在Excel中作为时间正常工作,它们需要被转换为日期时间对象。之后程序应该正常运行。以下是带有一些修复的示例:

import xlsxwriter
import datetime

workbook = xlsxwriter.Workbook('test4.xlsx')
worksheet1 = workbook.add_worksheet()

# 添加格式。浅红色背景,深红色文本。
format1 = workbook.add_format({'bg_color': '#FFC7CE', 'font_color': '#9C0006'})

# 添加格式。绿色背景,深绿色文本。
format2 = workbook.add_format({'bg_color': '#C6EFCE', 'font_color': '#006100'})

import pandas as pd

data = pd.DataFrame({'Time': [datetime.time(7, 2, 7), datetime.time(15 ,16, 55), 
                              datetime.time(15 ,17, 20), datetime.time(15 ,28, 58), 
                              datetime.time(15 ,32, 28), datetime.time(15 ,38, 54)],
                     'Payload': ['[0]->[1]', '[1]->[0]','[0]->[1]',
                                 '[0]->[1]','[1]->[0]','[0]->[1]']})

caption = ('Cells with values >= 08:00:00 are in light red. '
           'Values < 50 are in light green.')

# 写入数据。
worksheet1.write('A1', caption)

for row, row_data in enumerate(data):
    worksheet1.write(row + 2, 1, row_data)

time_format = workbook.add_format({'num_format': 'hh:mm::ss'})

if len(data) > 0:    
    for i in range(len(data)):
        worksheet1.write('D'+str(i+5), data['Time'].iloc[i], time_format)
        worksheet1.write('E'+str(i+5), data['Payload'].iloc[i])

# 在一定范围上写入条件格式。
worksheet1.conditional_format('D5:D12', {'type': 'date',
                                         'criteria': '>=',
                                         'value':  datetime.time(8, 0, 0),
                                         'format': format1})

workbook.close()

输出:

使用pandas比较时间,并根据结果更改字体。

英文:

There are a number of issue in the code but the main one is that the values like '07:02:07' are strings and not dates/times.

In order for them to work as times in Excel they need to be converted to datetime objects. After that the program should work. Here is an example with some fixes:

import xlsxwriter
import datetime

workbook = xlsxwriter.Workbook(&#39;test4.xlsx&#39;)
worksheet1 = workbook.add_worksheet()


# Add a format. Light red fill with dark red text.
format1 = workbook.add_format({&#39;bg_color&#39;: &#39;#FFC7CE&#39;,
                               &#39;font_color&#39;: &#39;#9C0006&#39;})

# Add a format. Green fill with dark green text.
format2 = workbook.add_format({&#39;bg_color&#39;: &#39;#C6EFCE&#39;,
                               &#39;font_color&#39;: &#39;#006100&#39;})


import pandas as pd

data= pd.DataFrame({&#39;Time&#39;: [datetime.time(7, 2, 7), datetime.time(15 ,16, 55), 
                             datetime.time(15 ,17, 20), datetime.time(15 ,28, 58), 
                             datetime.time(15 ,32, 28), datetime.time(15 ,38, 54)],
                    &#39;Payload&#39;: [&#39;[0]-&gt;[1]&#39;, &#39;[1]-&gt;[0]&#39;,&#39;[0]-&gt;[1]&#39;,
                                &#39;[0]-&gt;[1]&#39;,&#39;[1]-&gt;[0]&#39;,&#39;[0]-&gt;[1]&#39;]})

caption = (&#39;Cells with values &gt;= 08:00:00 are in light red. &#39;
           &#39;Values &lt; 50 are in light green.&#39;)

# Write the data.
worksheet1.write(&#39;A1&#39;, caption)

for row, row_data in enumerate(data):
    worksheet1.write(row + 2, 1, row_data)

time_format = workbook.add_format({&#39;num_format&#39;: &#39;hh:mm::ss&#39;})

if len (data) &gt;0:    
    for i in range (len(data)):

        worksheet1.write(&#39;D&#39;+str(i+5),(data[&#39;Time&#39;].iloc[i]), time_format)
        worksheet1.write(&#39;E&#39;+str(i+5),(data[&#39;Payload&#39;].iloc[i]))




# Write a conditional format over a range.
worksheet1.conditional_format(&#39;D5:D12&#39;, {&#39;type&#39;: &#39;date&#39;,
                                         &#39;criteria&#39;: &#39;&gt;=&#39;,
                                         &#39;value&#39;:  datetime.time(8, 0, 0),
                                         &#39;format&#39;: format1})

workbook.close()

Output:

使用pandas比较时间,并根据结果更改字体。

huangapple
  • 本文由 发表于 2020年1月6日 21:05:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612658.html
匿名

发表评论

匿名网友

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

确定