这个最小的Jupyternotebook Python脚本没有以彩色打印数据帧,我不确定为什么

2024-04-20 02:17:38 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的代码(我得到了from a different SO question)应该打印出一个带有一些彩色单元格的表。但是,它不会以彩色打印:

import pandas as pd
from IPython.core.display import HTML
from IPython.display import display

df = pd.DataFrame([[2,3,1], [3,2,2], [2,4,4]], columns=list("ABC"))

df.style.apply(lambda x: ["background: red" if v > x.iloc[0] else "" for v in x], axis = 1)
df
print(df)
display(df)
display(HTML(df.to_html()))
print('hi')

以下是一个屏幕截图: jupyter-notebook screenshot of script execution

但是,根据我上面链接的the SO example,它应该是这样的: example of what the table is supposed to look like

这可能是一些有用的信息:

(stats_env) raj@r-sb2:~/repos/fix_print$ jupyter --version
jupyter core     : 4.6.3
jupyter-notebook : 6.1.1
qtconsole        : not installed
ipython          : 7.17.0
ipykernel        : 5.3.4
jupyter client   : 6.1.6
jupyter lab      : not installed
nbconvert        : 5.6.1
ipywidgets       : not installed
nbformat         : 5.0.7
traitlets        : 4.3.3

Tags: installed代码fromcoreimportdfsohtml
1条回答
网友
1楼 · 发布于 2024-04-20 02:17:38

你又在打印df了! 评论最后一部分。它会起作用的

import pandas as pd
from IPython.core.display import HTML
from IPython.display import display

df = pd.DataFrame([[2,3,1], [3,2,2], [2,4,4]], columns=list("ABC"))

df.style.apply(lambda x: ['background: red' if v > x.iloc[0] else "" for v in x], axis = 1)
# df
# print(df)
# display(df)
# display(HTML(df.to_html()))

相关问题 更多 >