【发布时间】:2017-01-14 20:58:18
【问题描述】:
我一直在尝试编写一个与 pandas style 一起使用的函数。我想突出显示我在参数中指定的特定列。这不是很优雅,但是例如:
data = pd.DataFrame(np.random.randn(5, 3), columns=list('ABC'))
def highlight_cols(df, cols, colcolor = 'gray'):
for col in cols:
for dfcol in df.columns:
if col == cols:
color = colcolor
return ['background-color: %s' % color]*df.shape[0]
然后调用:
data.style.apply(highlight_cols(cols=['B','C']))
我收到一个错误:
'Series' object has no attribute 'columns'
我想我基本上不太了解样式器如何调用和applyies 函数。
【问题讨论】:
标签: python pandas pandas-styles