简体   繁体   English

带有CSS样式属性错误的Pandas数据框(非唯一多索引)到html表

[英]Pandas dataframe (non-unique multi-index) to html table with CSS styling attribute error

I am trying to export a pandas dataframe to a html table with custom CSS coloring options using a pandas Style object. 我正在尝试使用pandas Style对象将pandas数据帧导出到具有自定义CSS着色选项的html表中。 I have read through the examples listed here: http://pandas.pydata.org/pandas-docs/stable/style.html 我已阅读了以下示例: http : //pandas.pydata.org/pandas-docs/stable/style.html

I understand that I need to create a style object and then render that object. 我知道我需要创建一个样式对象,然后渲染该对象。

My issue is that my specific table is raising an exception: AttributeError: 'str' object has no attribute 'ndim' . 我的问题是我的特定表引发了一个异常: AttributeError: 'str' object has no attribute 'ndim' My table is different from the example tables in the link because it contains a non-unique multi-index. 我的表与链接中的示例表不同,因为它包含非唯一的多索引。

The simplest example of this ValueError can be created with this code: 可以使用以下代码创建此ValueError的最简单示例:

import pandas as pd
import numpy as np



myTable = pd.DataFrame({ 'Class': ['A', 'A', 'A', 'A', 'A'],
                    'First' : ['John', 'Lenny', 'Bill', 'Ryan', 'James'],
                    'Last': ['McGee', 'Beir', 'Schwarts', 'Mulroney', 'Buchura'],
                    'Grade': [76, 87, 46, 83, 98],
                    'StudentID': [2828, 2673, 7811, 2828, 1782]})

myTable.set_index(['Class', 'StudentID'], inplace=True)
myTable = myTable[['First', 'Last', 'Grade']] 

s = myTable.style

html = s.render()

text_file = open('df.html', "w")
text_file.write(s)
text_file.close()

Notice that the table contains a non-unique index (1st and 4th row). 请注意,该表包含一个非唯一索引(第一行和第四行)。

Moreover, if I get rid of the non-uniqueness, another exception is raised: AttributeError: 'str' object has no attribute 'ndim' . 而且,如果我摆脱了非唯一性,则会引发另一个异常: AttributeError: 'str' object has no attribute 'ndim' The example below will produce that error: 下面的示例将产生该错误:

import pandas as pd
import numpy as np



myTable = pd.DataFrame({ 'Class': ['A', 'A', 'A', 'A', 'A'],
                    'First' : ['John', 'Lenny', 'Bill', 'Ryan', 'James'],
                    'Last': ['McGee', 'Beir', 'Schwarts', 'Mulroney', 'Buchura'],
                    'Grade': [76, 87, 46, 83, 98],
                    'StudentID': [2828, 2673, 7811, 2878, 1782]})

myTable.set_index(['Class', 'StudentID'], inplace=True)
myTable = myTable[['First', 'Last', 'Grade']] 

s = myTable.style

html = s.render()

text_file = open('df.html', "w")
text_file.write(s)
text_file.close()

How does one get around these errors? 如何解决这些错误? Why would pandas limit styling options to unique 1-dimensional index DataFrames ? 为什么熊猫会将样式选项限制为唯一的一维索引DataFrames

The reason this doesn't work is because when you use MultiIndex and then change it to HTML, you will see that additional empty headers and multiple header rows are created. 这样做不起作用的原因是,当您使用MultiIndex并将其更改为HTML时,您会看到创建了其他空标题和多个标题行。

Style is not written for MultiIndexing. 没有为MultiIndexing编写样式。 You will likely need to replace data inside your rendered HTML instead of using pandas Style. 您可能需要替换呈现的HTML中的数据,而不是使用pandas样式。

You also don't have anything to style ... You sure you don't just need .to_html() added? 您也没有任何样式可言...您确定不只是需要添加.to_html()吗?

import pandas as pd
import numpy as np

myTable = pd.DataFrame({ 'Class': ['A', 'A', 'A', 'A', 'A'],
                'First' : ['John', 'Lenny', 'Bill', 'Ryan', 'James'],
                'Last': ['McGee', 'Beir', 'Schwarts', 'Mulroney', 'Buchura'],
                'Grade': [76, 87, 46, 83, 98],
                'StudentID': [2828, 2673, 7811, 2878, 1782]})

myTable.set_index(['Class', 'StudentID'], inplace=True)
myTable = myTable[['First', 'Last', 'Grade']] 

html = myTable.to_html()

text_file = open('df.html', "w")
text_file.write(html)
text_file.close()

OPTION 2 选项2

I also saw this post for something similar and a suggestion was to use pd.IndexSlice 我也看到了这篇类似文章 ,建议使用pd.IndexSlice

OPTION 3 选项3

Changing your CSS styles 更改CSS样式

At the beginning of your HTML, you can add something like: 在HTML的开头,您可以添加以下内容:

<style>
table {text-align: center;}
table thead th {text-align: center;}
</style>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 需要处理具有非唯一多索引的串联数据帧 - Need to handle a concatenated dataframe with non-unique multi-index Dataframe.loc 返回字典或 Dataframe [已解决](无法处理非唯一的多索引!) - Dataframe.loc returns dictionary or a Dataframe [Solved] (Cannot handle a non-unique multi-index!) pandas.concat:无法处理非唯一的多索引! 熊猫蟒 - pandas.concat: Cannot handle a non-unique multi-index! Pandas Python Pandas ValueError:尝试重新索引时无法处理非唯一多索引 - Pandas ValueError: cannot handle a non-unique multi-index when trying to re-index 获取 ValueError - 在 Pandas 中使用 expand() 时出现非唯一多索引 - Getting ValueError - non-unique multi-index when using explode() in Pandas 这是什么意思? xarray 错误:无法处理非唯一的多索引 - what does this mean? xarray error: cannot handle a non-unique multi-index ValueError:无法处理非唯一的多索引! 尝试将来自多个数据帧的列合并到一个 dataframe - ValueError: cannot handle a non-unique multi-index! When trying to combine columns from multiple dataframes in to one dataframe ValueError:无法处理非唯一的多索引! 即使我们有唯一索引 - ValueError: cannot handle a non-unique multi-index! even when we have unique index 如何合并/合并/加入 2 个具有非唯一多索引的数据帧以协调内容? - how to merge/concat/join 2 dataframes with a non-unique multi-index to reconcile the content? 使用非唯一索引从pandas DataFrame中删除行 - Delete rows from pandas DataFrame with non-unique index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM