【发布时间】:2020-02-21 12:32:19
【问题描述】:
尝试在样式中使用背景颜色不知道这是否正确
我认为我应该使用 if elif 语句,但它也会给我错误。我想我必须对我感兴趣的特定列使用 loc 或 iloc 因为有不同的列
这段代码主要是
ValueError: Function <function flt_cat_style_function_1 at 0x7f08ea52b830> returned the wrong shape.
Result has shape: (1,)
Expected shape: (11, 1)
a=df['flt_cat']
def flt_cat_style_function_1(a):
df['flt_cat'].str.contains(r'VLIFR','background-color: #9400D3')
df['flt_cat'].str.contains(r'LIFR','background-color: #FFA500')
df['flt_cat'].str.contains(r'IFR','background-color: #FF0000')
df['flt_cat'].str.contains(r'MVFR','background-color: #FFFF00')
df['flt_cat'].str.contains(r'VFR','background-color: #00b050')
highlighted=df.style.apply(flt_cat_style_function_1,subset='flt_cat').render()
0 VLIFR
1 LIFR
2 LIFR
3 LIFR
4 IFR
5 IFR
6 MVFR
7 MVFR
8 MVFR
9 MVFR
10 VFR
Name: flt_cat, dtype: object
with open('shtml.html','w') as f:
f.write(highlighted)
【问题讨论】:
标签: python html css pandas styles