在Chrome或Edge中,大型数据框中的所有行均未显示熊猫样式

更新:

  • 问题似乎在于在 Google Chrome 和 Microsoft Edge 中显示带有 Pandas 呈现的样式的 HTML。

  • Firefox 中的 JupyterLab 正确显示所有样式行并正确呈现输出 HTML 文件。

  • 更新的问题是

    1. 为什么pandas 渲染的HTML 不能完全显示Chrome 或Edge 中的所有样式?
    2. 有没有更有效的方法来应用 Pandas 完成的样式,以便 HTML 也适用于 Chrome 和 Edge?
  • 版本:

    • pandas v1.2.4
    • Chrome v90.0.4430.93 (Official Build) (64-bit)
    • Edge v90.0.818.56 (Official build) (64-bit)

原来的:

  • 问题 - 它们是相互关联的:
    1. 为什么不是所有行都在 Jupyter 中显示背景样式或写入 HTML?
      • 所有行都应具有绿色样式,但最后 5 行不显示样式。
    2. 如何使所有行显示背景样式?
  • 给定一个大数据框,在这种情况下474 rows x 35 columns,应用的样式停止显示。
  • 如果行数或列数增加超过此大小,则不会显示更多行。
  • 我们可以从样式映射中看到,行已正确映射到背景颜色,但未显示。
  • 如果行数或列数减少,则所有行都显示正确的样式。
  • 测试在 jupyterlab v3.0.11
  • PyCharm 2021.1 (Professional Edition) Build #PY-211.6693.115, built on April 6, 2021将重新渲染的样式器保存到文件中进行了测试,结果相同,因此这不仅仅是jupyter.
  • 在控制台中测试
  • 这个问题可以在我尝试过的两个不同系统上重现。
  • 如果形状缩小为471 rows × 35 columns474 rows × 34 columns,则所有行都会正确显示突出显示。
  • 相关的熊猫错误报告:40913

可重现的数据帧

import pandas as pd
import numpy as np
from faker import Faker  # conda install -c conda-forge faker or pip install Faker

# for fake names
fake = Faker()

# test data
np.random.seed(365)
rows = 11000

# change 36 or 158 to test where the rows stop appearing
vals = {f'val{i}': np.random.randint(1, 11, size=(rows)) for i in range(1, 36)}
data = {'name': np.random.choice([fake.unique.name() for i in range(158)], size=rows),
        'cat': np.random.randint(1, 4, size=(rows))}
data.update(vals)

df = pd.DataFrame(data)

# used to create the mask for the background color
mean = df.groupby('cat').mean().round(2)

# calculate the mean for each name and cat
cat_mean = df.groupby(['name', 'cat']).mean()


def color(x):
    """Function to apply background color"""
    c1 = 'background-color: green'
    c = '' 
    # compare columns
    mask1 = x.gt(mean)
    # DataFrame with same index and columns names as original filled empty strings
    df1 =  pd.DataFrame(c, index=x.index, columns=x.columns)
    # modify values of df1 column by boolean mask
    df1[mask1] = c1
    display(df1)

    return df1


# Last line in notebook displays the styled dataframe
cat_mean.style.apply(color, axis=None)

# Last line in PyCharm saving rendered styler to file - comment out when in Jupyter
cm = cat_mean.style.apply(color, axis=None).set_precision(3).render()

# save the output to an html file
with open('cm_test.html', 'w') as f:
    f.write(cm)

参考

  • 应用背景颜色的函数引用了此答案。
  • 这个问题类似,但没有答案。

输出 pd.show_versions()

  • None未显示所有包以节省空间
INSTALLED VERSIONS
------------------
commit           : f2c8480af2f25efdbd803218b9d87980f416563e
python           : 3.8.8.final.0 or 3.9.2.final.0
python-bits      : 64
OS               : Windows
OS-release       : 10
Version          : 10.0.19041
machine          : AMD64
processor        : Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder        : little
LOCALE           : English_United States.1252

pandas           : 1.2.3 or 1.2.4
numpy            : 1.19.2
pytz             : 2021.1
dateutil         : 2.8.1
pip              : 21.0.1
setuptools       : 52.0.0.post20210125
Cython           : 0.29.22
pytest           : 6.2.3
sphinx           : 3.5.3
xlsxwriter       : 1.3.8
lxml.etree       : 4.6.3
html5lib         : 1.1
jinja2           : 2.11.3
IPython          : 7.22.0
pandas_datareader: 0.9.0
bs4              : 4.9.3
bottleneck       : 1.3.2
fsspec           : 0.9.0
matplotlib       : 3.3.4
numexpr          : 2.7.3
openpyxl         : 3.0.7
scipy            : 1.6.2
sqlalchemy       : 1.4.5
tables           : 3.6.1
tabulate         : 0.8.9
xlrd             : 2.0.1
xlwt             : 1.3.0
numba            : 0.53.1
INSTALLED VERSIONS
------------------
commit           : f2c8480af2f25efdbd803218b9d87980f416563e
python           : 3.8.8.final.0 or 3.9.2.final.0
python-bits      : 64
OS               : Windows
OS-release       : 10
Version          : 10.0.19041
machine          : AMD64
processor        : Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder        : little
LOCALE           : English_United States.1252

pandas           : 1.2.3 or 1.2.4
numpy            : 1.19.2
pytz             : 2021.1
dateutil         : 2.8.1
pip              : 21.0.1
setuptools       : 52.0.0.post20210125
Cython           : 0.29.22
pytest           : 6.2.3
sphinx           : 3.5.3
xlsxwriter       : 1.3.8
lxml.etree       : 4.6.3
html5lib         : 1.1
jinja2           : 2.11.3
IPython          : 7.22.0
pandas_datareader: 0.9.0
bs4              : 4.9.3
bottleneck       : 1.3.2
fsspec           : 0.9.0
matplotlib       : 3.3.4
numexpr          : 2.7.3
openpyxl         : 3.0.7
scipy            : 1.6.2
sqlalchemy       : 1.4.5
tables           : 3.6.1
tabulate         : 0.8.9
xlrd             : 2.0.1
xlwt             : 1.3.0
numba            : 0.53.1

解决方法

拆分数据帧

  • 这令人不满意,但是拆分 DataFrame 并应用样式会起作用,因为如果减小了整体大小。

保存到 Excel

  • 保存到 Excel 时,所有行均以突出显示颜色正确显示
cat_mean.iloc[:237, :].style.apply(color, axis=None)
cat_mean.iloc[237:, :].style.apply(color, axis=None)

以上是在Chrome或Edge中,大型数据框中的所有行均未显示熊猫样式的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>