使用 Pandas 的 Excel 文件的背景颜色:单列边界问题(对于异常值)
我需要导出一个 Excel,其中数据集的异常值以黄色突出显示。如您所知,通过上限和下限计算。
我已经成功地设置了该函数,但我只是无法让它以迭代的方式工作,因此边界是针对每一列的。
当我将数据框导出到 Excel 文件时,它仅根据一个变量的异常值对单元格进行着色。它在迭代模式下不起作用。
这是我的代码,我哪里错了?
在这里,我只是计算每列的下限和上限(主题 ID 除外),
for col in dfm.columns.difference(['Sbj']):
Q1c = dfm[col].quantile(0.25)
Q3c = dfm[col].quantile(0.75)
IQRc = Q3 - Q1
lowc = Q1-1.5*IQR
uppc = Q3+1.5*IQR
我将函数设置为基于极限值(未迭代)为单个值着色,
def color(v):
if v < lowc or v > uppc:
color = 'yellow'
return 'background-color: %s' % color
对每列迭代应用该函数,
for col in dfm.columns.difference(['Sbj']):
df_colored = dfm.style.applymap(color)
很明显,有些东西是迭代错误。
非常感谢!
I need to export an excel where the outliers of my dataset are highlighted in yellow. As you know, calculated with upper bound and lower bound.
I've managed to set up the function, I just can't get it to work in an iterated way so the bounds are for each column.
When I export the dataframe into an excel file, it colours the cells based on the outliers of only one variable. It doesn't work in iterated mode.
Here my code, where am I wrong?
Here I just calculate for each column the lower and the upper bound (except for Subject ID)
for col in dfm.columns.difference(['Sbj']):
Q1c = dfm[col].quantile(0.25)
Q3c = dfm[col].quantile(0.75)
IQRc = Q3 - Q1
lowc = Q1-1.5*IQR
uppc = Q3+1.5*IQR
I set the function to colour the single value based on the limit values (not iterated)
def color(v):
if v < lowc or v > uppc:
color = 'yellow'
return 'background-color: %s' % color
apply the function iteratively for each column
for col in dfm.columns.difference(['Sbj']):
df_colored = dfm.style.applymap(color)
It is obvious that something is wrong with the iteration.
Many thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论