【问题标题】:Is there a way to color values in a dataframe if the belong to a certain range (Python-Pandas)如果属于某个范围(Python-Pandas),有没有办法为数据框中的值着色
【发布时间】:2017-07-10 13:47:16
【问题描述】:

我有一个值从 0 到 10 的数据框。我想用红色而不是黑色为值 1 和 5 着色。这可以在python DataFrame中做到吗?我正在使用 Jupyter 笔记本。

【问题讨论】:

  • notebook的python端没有办法。你可以做的是改变事物的 javascript 方面。

标签: python-3.x pandas jupyter-notebook


【解决方案1】:

您可以更改单元格的style -

df = pd.DataFrame({'v1': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]})
dft = df.style.applymap(lambda x: 'color: red' if x >= 1 and x <=5 else 'color: black')
dft

您可以在此处找到有关应用样式的更多信息 - http://pandas.pydata.org/pandas-docs/stable/style.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 2017-04-07
    • 1970-01-01
    相关资源
    最近更新 更多