久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<i id='WnAiD'><tr id='WnAiD'><dt id='WnAiD'><q id='WnAiD'><span id='WnAiD'><b id='WnAiD'><form id='WnAiD'><ins id='WnAiD'></ins><ul id='WnAiD'></ul><sub id='WnAiD'></sub></form><legend id='WnAiD'></legend><bdo id='WnAiD'><pre id='WnAiD'><center id='WnAiD'></center></pre></bdo></b><th id='WnAiD'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='WnAiD'><tfoot id='WnAiD'></tfoot><dl id='WnAiD'><fieldset id='WnAiD'></fieldset></dl></div>
    • <bdo id='WnAiD'></bdo><ul id='WnAiD'></ul>
    <tfoot id='WnAiD'></tfoot>

    <legend id='WnAiD'><style id='WnAiD'><dir id='WnAiD'><q id='WnAiD'></q></dir></style></legend>
  • <small id='WnAiD'></small><noframes id='WnAiD'>

      1. Python - 使用 pandas 格式化 Excel 單元格

        Python - Using pandas to format excel cell(Python - 使用 pandas 格式化 Excel 單元格)
          <i id='Y7SJ0'><tr id='Y7SJ0'><dt id='Y7SJ0'><q id='Y7SJ0'><span id='Y7SJ0'><b id='Y7SJ0'><form id='Y7SJ0'><ins id='Y7SJ0'></ins><ul id='Y7SJ0'></ul><sub id='Y7SJ0'></sub></form><legend id='Y7SJ0'></legend><bdo id='Y7SJ0'><pre id='Y7SJ0'><center id='Y7SJ0'></center></pre></bdo></b><th id='Y7SJ0'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Y7SJ0'><tfoot id='Y7SJ0'></tfoot><dl id='Y7SJ0'><fieldset id='Y7SJ0'></fieldset></dl></div>

            • <bdo id='Y7SJ0'></bdo><ul id='Y7SJ0'></ul>
                <tbody id='Y7SJ0'></tbody>

              <small id='Y7SJ0'></small><noframes id='Y7SJ0'>

            • <legend id='Y7SJ0'><style id='Y7SJ0'><dir id='Y7SJ0'><q id='Y7SJ0'></q></dir></style></legend>
            • <tfoot id='Y7SJ0'></tfoot>

                  本文介紹了Python - 使用 pandas 格式化 Excel 單元格的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我有一個 pandas 數據框,如下所示.

                  I have a pandas dataframe, which is something like shown below.

                  我想將通過/失敗"列格式化為 if Fail -->紅色背景,否則綠色背景,如:

                  I would like to format the column "Pass/Fail" as if Fail --> red background, else green background, like:

                  我嘗試使用 Pandas 進行格式化,但無法為 Excel 添加顏色.以下是代碼:

                  I have tried to use Pandas to do the formatting, but it fails to add color to the excel. Following is the code:

                  writer = pandas.ExcelWriter(destination,engine = 'xlsxwriter')
                  color = Answer.style.applymap(lambda x: 'color: red' if x == "Fail" else 'color: green',subset= pandas.IndexSlice[:,['Pass/Fail']])
                  color.to_excel(writer,'sheet1')
                  

                  我嘗試了無法安裝的 StyleFrame.似乎 StyleFrame 不符合我的 python 3.6 版.

                  I tried StyleFrame which failed to install. Seems that StyleFrame does not comply with my python version 3.6.

                  如何根據需要格式化 Excel?

                  How can I format the excel as I want?

                  推薦答案

                  你可以使用 conditional_format:

                  df = pd.DataFrame({'Pass/Fail':['Pass','Fail','Fail'],
                                     'expect':[1,2,3]})
                  print (df)
                    Pass/Fail  expect
                  0      Pass       1
                  1      Fail       2
                  2      Fail       3
                  
                  writer = pd.ExcelWriter('pandas_conditional.xlsx', engine='xlsxwriter')
                  df.to_excel(writer, sheet_name='Sheet1')
                  workbook  = writer.book
                  worksheet = writer.sheets['Sheet1']
                  red_format = workbook.add_format({'bg_color':'red'})
                  green_format = workbook.add_format({'bg_color':'green'})
                  
                  worksheet.conditional_format('B2:B4', {'type': 'text',
                                                        'criteria': 'containing',
                                                         'value':     'Fail',
                                                         'format': red_format})
                  
                  worksheet.conditional_format('B2:B4', {'type': 'text',
                                                        'criteria': 'containing',
                                                         'value':   'Pass',
                                                         'format':  green_format})
                  writer.save()
                  

                  更動態的解決方案 get_loc 用于 column 的位置和與 dictionary 的映射:

                  More dynamic solution with get_loc for position of column and mapping with dictionary:

                  import string
                  
                  df = pd.DataFrame({'Pass/Fail':['Pass','Fail','Fail'],
                                     'expect':[1,2,3]})
                  print (df)
                    Pass/Fail  expect
                  0      Pass       1
                  1      Fail       2
                  2      Fail       3
                  

                  <小時>

                  writer = pd.ExcelWriter('pandas_conditional.xlsx', engine='xlsxwriter')
                  df.to_excel(writer, sheet_name='Sheet1')
                  workbook  = writer.book
                  worksheet = writer.sheets['Sheet1']
                  red_format = workbook.add_format({'bg_color':'red'})
                  green_format = workbook.add_format({'bg_color':'green'})
                  
                  #dict for map excel header, first A is index, so omit it
                  d = dict(zip(range(25), list(string.ascii_uppercase)[1:]))
                  print (d)
                  {0: 'B', 1: 'C', 2: 'D', 3: 'E', 4: 'F', 5: 'G', 6: 'H', 7: 'I', 8: 'J',
                   9: 'K', 10: 'L', 11: 'M', 12: 'N', 13: 'O', 14: 'P', 15: 'Q', 16: 'R', 
                   17: 'S', 18: 'T', 19: 'U', 20: 'V', 21: 'W', 22: 'X', 23: 'Y', 24: 'Z'}
                  
                  #set column for formatting
                  col = 'Pass/Fail'
                  excel_header = str(d[df.columns.get_loc(col)])
                  #get length of df
                  len_df = str(len(df.index) + 1)
                  rng = excel_header + '2:' + excel_header + len_df
                  print (rng)
                  B2:B4
                  
                  worksheet.conditional_format(rng, {'type': 'text',
                                                        'criteria': 'containing',
                                                         'value':     'Fail',
                                                         'format': red_format})
                  
                  worksheet.conditional_format(rng, {'type': 'text',
                                                        'criteria': 'containing',
                                                         'value':   'Pass',
                                                         'format':  green_format})
                  writer.save()
                  

                  謝謝jmcnamara供評論和 XlsxWriter

                  col = 'Pass/Fail'
                  loc = df.columns.get_loc(col) + 1
                  len_df = len(df.index) + 1
                  
                  worksheet.conditional_format(1,loc,len_df,loc, {'type': 'text',
                                                        'criteria': 'containing',
                                                         'value':     'Fail',
                                                         'format': red_format})
                  
                  worksheet.conditional_format(1,loc,len_df,loc, {'type': 'text',
                                                        'criteria': 'containing',
                                                         'value':   'Pass',
                                                         'format':  green_format})
                  writer.save()
                  

                  最后一個版本的 pandas (0.20.1) 和 樣式:

                  Another solution with last version of pandas (0.20.1) and styles:

                  df = pd.DataFrame({'Pass/Fail':['Pass','Fail','Fail'],
                                     'expect':['d','f','g']})
                  print (df)
                    Pass/Fail expect
                  0      Pass      d
                  1      Fail      f
                  2      Fail      g
                  
                  def f(x):
                      col = 'Pass/Fail'
                      r = 'background-color: red'
                      g = 'background-color: green'
                      c = np.where(x[col] == 'Pass', g, r)
                      y = pd.DataFrame('', index=x.index, columns=x.columns)
                      y[col] = c
                      return y
                  
                  styled = df.style.apply(f, axis=None)
                  styled.to_excel('styled.xlsx', engine='openpyxl')
                  

                  這篇關于Python - 使用 pandas 格式化 Excel 單元格的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How to make a discord bot that gives roles in Python?(如何制作一個在 Python 中提供角色的不和諧機器人?)
                  Discord bot isn#39;t responding to commands(Discord 機器人沒有響應命令)
                  Can you Get the quot;About mequot; feature on Discord bot#39;s? (Discord.py)(你能得到“關于我嗎?Discord 機器人的功能?(不和諧.py))
                  message.channel.id Discord PY(message.channel.id Discord PY)
                  How do I host my discord.py bot on heroku?(如何在 heroku 上托管我的 discord.py 機器人?)
                  discord.py - Automaticaly Change an Role Color(discord.py - 自動更改角色顏色)

                    <small id='Vlhwd'></small><noframes id='Vlhwd'>

                        <legend id='Vlhwd'><style id='Vlhwd'><dir id='Vlhwd'><q id='Vlhwd'></q></dir></style></legend>

                      1. <tfoot id='Vlhwd'></tfoot>

                          <tbody id='Vlhwd'></tbody>
                        • <bdo id='Vlhwd'></bdo><ul id='Vlhwd'></ul>
                          1. <i id='Vlhwd'><tr id='Vlhwd'><dt id='Vlhwd'><q id='Vlhwd'><span id='Vlhwd'><b id='Vlhwd'><form id='Vlhwd'><ins id='Vlhwd'></ins><ul id='Vlhwd'></ul><sub id='Vlhwd'></sub></form><legend id='Vlhwd'></legend><bdo id='Vlhwd'><pre id='Vlhwd'><center id='Vlhwd'></center></pre></bdo></b><th id='Vlhwd'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Vlhwd'><tfoot id='Vlhwd'></tfoot><dl id='Vlhwd'><fieldset id='Vlhwd'></fieldset></dl></div>

                            主站蜘蛛池模板: 免费看黄色视屏 | 99视频在线播放 | 国产一区视频在线 | 亚洲色图综合网 | 精品国产乱码久久久久久闺蜜 | 91视频精选 | 欧美在线视频一区二区 | 99资源| 久久久精| 免费av在线 | 国产精品视频一区二区三区 | 久久精品视频91 | 亚洲一区欧美一区 | 国产精品综合久久 | 亚洲免费精品 | 成人免费观看男女羞羞视频 | 色婷婷精品久久二区二区蜜臂av | 一区二区三区在线播放 | 日韩欧美在线视频 | 产真a观专区 | 亚洲精品视频在线观看视频 | 欧美一级毛片在线播放 | 精品成人佐山爱一区二区 | 永久看片 | 亚洲午夜av久久乱码 | 久久久久久国产精品 | 日本精品视频在线观看 | 日韩欧美在线播放 | 日韩中文av在线 | 亚洲欧美在线观看 | 亚洲成人高清 | 国产成人精品久久 | 黄色毛片在线观看 | 日韩视频精品在线 | 狠狠干av | 九九久久精品视频 | 免费毛片网站 | 黄色在线免费看 | 成人免费网站www网站高清 | 久久人| 天天看逼 |