我正在尝试使用熊猫将列中的所有行格式化为“XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX”格式,但失败了。 (输入示例:954E47384F568F91851E1BABE25850XX)有没有人知道最好的方法? 感谢您的时间! 我收到错误 TypeError: 'Series ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我有以下数据框,并希望将其转换为HTML
Limit Status Warning 3M AVG
VAR1 1.20 1.21216 1.11 1.21235
VAR2 0.82 0.63075 0.75 0.593295
VAR3 0.38 0.376988 0.35 0.376988
VAR4 0.17 0.126987 0.14 0.12461
我想逐行格式化这个数据帧,以便:
Status
超过Warning
,整行将突出显示为黄色,如果超过Limit
则整行将突出显示为红色 VAR2
和VAR3
具有“{:。2%}”格式, VAR1
和VAR4
具有“{:。2f}” 我已经挖掘了pandas文档并尝试了几种方法,但我无法完成上述所有任务
如果您能提供帮助,我将不胜感激,因为我认为对于许多大熊猫用户而言,行格式化数据帧是一项挑战。
编辑1:我尝试了以下代码:
df=df.transpose()
df.style.format("{:.2%}").format({"VAR1":"{:.2f},"VAR4":"{:.2f}"})
注意:通过转置数据框,可以更轻松地完成所有任务,但我无法将其转换回原始形状,因为它是样式器。
我认为您可以使用自定义样式功能执行您想要的操作:
def color(row):
if row.Status >= row.Limit:
return ['background-color: red'] * len(row)
elif row.Status >= row.Warning:
return ['background-color: yellow'] * len(row)
return [''] * len(row)
df.style.apply(color, axis=1)
但是,您仍然需要为此添加自定义数字格式。
要获取此代码的HTML代码,请使用render
方法:
df.style.apply(color, axis=1).render()
<style type="text/css" > #T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow0_col0 { background-color: red; } #T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow0_col1 { background-color: red; } #T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow0_col2 { background-color: red; } #T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow0_col3 { background-color: red; } #T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow2_col0 { background-color: yellow; } #T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow2_col1 { background-color: yellow; } #T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow2_col2 { background-color: yellow; } #T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow2_col3 { background-color: yellow; }</style> <table id="T_e61b55e0_cef5_11e8_9f07_68f72880acdc" > <thead> <tr> <th class="blank level0" ></th> <th class="col_heading level0 col0" >Limit</th> <th class="col_heading level0 col1" >Status</th> <th class="col_heading level0 col2" >Warning</th> <th class="col_heading level0 col3" >3M AVG</th> </tr></thead> <tbody> <tr> <th id="T_e61b55e0_cef5_11e8_9f07_68f72880acdclevel0_row0" class="row_heading level0 row0" >VAR1</th> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow0_col0" class="data row0 col0" >1.2</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow0_col1" class="data row0 col1" >1.21216</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow0_col2" class="data row0 col2" >1.11</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow0_col3" class="data row0 col3" >1.21235</td> </tr> <tr> <th id="T_e61b55e0_cef5_11e8_9f07_68f72880acdclevel0_row1" class="row_heading level0 row1" >VAR2</th> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow1_col0" class="data row1 col0" >0.82</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow1_col1" class="data row1 col1" >0.63075</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow1_col2" class="data row1 col2" >0.75</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow1_col3" class="data row1 col3" >0.593295</td> </tr> <tr> <th id="T_e61b55e0_cef5_11e8_9f07_68f72880acdclevel0_row2" class="row_heading level0 row2" >VAR3</th> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow2_col0" class="data row2 col0" >0.38</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow2_col1" class="data row2 col1" >0.376988</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow2_col2" class="data row2 col2" >0.35</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow2_col3" class="data row2 col3" >0.376988</td> </tr> <tr> <th id="T_e61b55e0_cef5_11e8_9f07_68f72880acdclevel0_row3" class="row_heading level0 row3" >VAR4</th> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow3_col0" class="data row3 col0" >0.17</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow3_col1" class="data row3 col1" >0.126987</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow3_col2" class="data row3 col2" >0.14</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow3_col3" class="data row3 col3" >0.12461</td> </tr></tbody> </table>
我有同样的问题,并研究pandas.io.formats.style.Styler
类中的format
函数的实现,并实现了类似的行方式函数:
def format_row_wise(styler, formatter):
for row, row_formatter in formatter.items():
row_num = styler.index.get_loc(row)
for col_num in range(len(styler.columns)):
styler._display_funcs[(row_num, col_num)] = row_formatter
return styler
示例 :
df = pandas.DataFrame(
{
'Limit': [1.20, 0.82, 0.38, 0.17],
'Status': [1.21216, 0.63075, 0.376988, 0.126987],
'Warning': [1.11, 0.75, 0.35, 0.14],
'3M AVG': [1.21235, 0.593259, 0.376988, 0.12461]
},
index=['VAR1', 'VAR2', 'VAR3', 'VAR4']
)
formatters = {"VAR1":lambda x: f"{x:.2f}", "VAR4": lambda x: f"{x:.2f}"}
styler = format_row_wise(df.style, formatters)
styler.render()
这对我有用:)
注意 :
希望这能让你走上正确的道路......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.