为Pandas Dataframe.to_html()的单列着色 [英] Coloring Single Column of Pandas Dataframe.to_html()
问题描述
在将其标记为重复之前,我已经尝试了以下主题中的代码,到目前为止,没有一个对我有用:
Before this is marked as duplicate, I have tried the code from the following topics and none has worked for me thus far:
我有生成三个熊猫数据框的代码,如下所示:
I have code that produces three pandas dataframe that looks like this:
RowName Orders Market StartTime StopTime
Status
good A 9 gold 10:00:00 10:09:45
.
.
.
bad B 60 silver 07:54:43 08:02:12
RowName Orders Market StartTime StopTime
Status
good E 19 plat. 10:00:00 10:09:45
.
.
bad F 54 mercury 07:54:43 08:02:12
RowName Orders Market StartTime StopTime
Status
great D 3 alum. 10:00:00 10:09:45
.
.
ok C 70 bronze 07:54:43 08:02:12
其中Status
列设置为每一帧的索引
where the Status
column is set as the index of each frame
对于每一帧,无论给定单元格中的值是多少,我都想突出显示StartTime
列,其值为#D42A2A
(又名红色).
For each frame, I want to highlight the StartTime
column with the value #D42A2A
(aka red) regardless of what value is in a given cell.
这怎么办?
最近失败的尝试:
-
def column_style(col): if col.Name == 'StartTime': return pd.Series('bgcolor: #d42a2a', col.index)
def column_style(col): if col.Name == 'StartTime': return pd.Series('bgcolor: #d42a2a', col.index)
def col_color(data):
color = 'red' if data != '' else 'black'
return 'color: %s' %color
frame.style.applymap(col_color, subset=['StartTime'])
def col_color(data):
color = 'red' if data != '' else 'black'
return 'color: %s' %color
frame.style.applymap(col_color, subset=['StartTime'])
但这也失败了.
注意:
-
我正在Linux shell中使用VI
I am using VI within a linux shell
整个脚本正在由IE(互联网浏览器)调用,因此脚本的输出为html
The entire script is being called by IE (internet explorer) so the output of the script is html
我正在使用BS(beautifulsoup)从几个站点抓取数据并将结果汇总到一页上
{*在抓取初始网站并创建所需的网站(称为Page1)之后,我试图在同一脚本中抓取Page1并通过.attrs
方法添加html行,但是此操作失败",即Web服务器时间在运行中}}
I am using BS (beautifulsoup) to scrape data from a few sites and the aggregating the results onto one page
{*after scraping the initial website and creating the required website (call it Page1), I tried to scrape Page1 in the same script and add in the html lines via the .attrs
method, but this "fails", i.e. the webserver times out during the run}
推荐答案
让我们尝试一下:
import pandas as pd
import numpy as np
np.random.seed(24)
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],
axis=1)
df.iloc[0, 2] = np.nan
def highlight_column(s, col):
return ['background-color: #d42a2a' if s.name == col else '' for v in s.index]
df.style.apply(highlight_column, col = 'B')
输出:
这篇关于为Pandas Dataframe.to_html()的单列着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!