将 header/description 部分添加到数据框 Pandas Python
Adding a header/description section to a dataframe Pandas Python
我想在下方数据框 data
的标题下方放置一个 header
值并获得预期输出,我该怎么做?
import pandas as pd
class SubclassedDataFrame(pd.DataFrame):
# normal properties
_metadata = ['description']
@property
def _constructor(self):
return SubclassedDataFrame
data = {"a": [1, 2, 3], "b": [10, 12, 13], "c": [230, 1, 3],"d": [8, 12, 9],}
df = SubclassedDataFrame(data)
title = 'Outputs'
header = 'header'
df = df.style.set_caption(title).set_table_styles([{
'selector': 'caption',
'props': [
('color', 'red'),
('font-size', '15px'),
('font-style', 'italic'),
('font-weight', 'bold'),
('text-align', 'center')
]
}])
display(df)
输出:
预期输出:
header
有两种方法:
一种方法是创建一个 header 为顶级的 MultiIndex:
title = 'Outputs'
header = 'header'
# Add MultiIndex Header
df.columns = pd.MultiIndex.from_product([[header], df.columns])
styler = df.style.set_caption(title).set_table_styles([{
'selector': 'caption',
'props': [
('color', 'red'),
('font-size', '15px'),
('font-style', 'italic'),
('font-weight', 'bold'),
('text-align', 'center')
]
}])
display(styler)
这种方法保持 pandas Styler object 的完整性,并使“header”成为 table
object 的一部分,成为thead
.
中的第一行
第二种方法是创建 Styler Subclass object 并修改 jinja
模板。
templates/myhtml.tpl
:
{% extends "html_table.tpl" %}
{% block table %}
<div class="wrap">
{% if table_title %}
<h1 class="table-title">{{ table_title}}</h1>
{% endif %}
{% if table_header %}
<h2 class="table-header">{{ table_header }}</h2>
{% endif %}
{{ super() }}
</div>
{% endblock table %}
templates/mystyles.tpl
:
{% extends "html_style.tpl" %}
{% block style %}
{{ super() }}
<style>
.wrap {
display: inline-block;
}
.table-title {
color: red;
font-size: 15px !important;
font-style: italic;
font-weight: bold;
text-align: center;
margin: 0 2px !important;
}
.table-header {
text-align: center;
font-size: 13px !important;
margin: 0 2px !important;
}
</style>
{% endblock style %}
title = 'Outputs'
header = 'header'
# Build Styler Subclass from templates
MyStyler = Styler.from_custom_template(
"templates", # Folder to Search
html_table="myhtml.tpl", # HTML Template
html_style='mystyles.tpl' # CSS Template
)
# Get Styler For `df`
styler = MyStyler(df)
# Render with arguments and display
HTML(styler.render(table_title=title, table_header=header))
这种方法将一些控制从 Styler object 转移到模板,但允许更大的灵活性,因为它可以用于创建额外的 HTML 元素,这些元素目前不受支持Styler object.
环境jupyter-notebook
设置:
import pandas as pd
from IPython.display import HTML, display
from pandas.io.formats.style import Styler
class SubclassedDataFrame(pd.DataFrame):
# normal properties
_metadata = ['description']
@property
def _constructor(self):
return SubclassedDataFrame
data = {
"a": [1, 2, 3],
"b": [10, 12, 13],
"c": [230, 1, 3],
"d": [8, 12, 9]
}
df = SubclassedDataFrame(data)
我想在下方数据框 data
的标题下方放置一个 header
值并获得预期输出,我该怎么做?
import pandas as pd
class SubclassedDataFrame(pd.DataFrame):
# normal properties
_metadata = ['description']
@property
def _constructor(self):
return SubclassedDataFrame
data = {"a": [1, 2, 3], "b": [10, 12, 13], "c": [230, 1, 3],"d": [8, 12, 9],}
df = SubclassedDataFrame(data)
title = 'Outputs'
header = 'header'
df = df.style.set_caption(title).set_table_styles([{
'selector': 'caption',
'props': [
('color', 'red'),
('font-size', '15px'),
('font-style', 'italic'),
('font-weight', 'bold'),
('text-align', 'center')
]
}])
display(df)
输出:
预期输出:
header
有两种方法:
一种方法是创建一个 header 为顶级的 MultiIndex:
title = 'Outputs'
header = 'header'
# Add MultiIndex Header
df.columns = pd.MultiIndex.from_product([[header], df.columns])
styler = df.style.set_caption(title).set_table_styles([{
'selector': 'caption',
'props': [
('color', 'red'),
('font-size', '15px'),
('font-style', 'italic'),
('font-weight', 'bold'),
('text-align', 'center')
]
}])
display(styler)
这种方法保持 pandas Styler object 的完整性,并使“header”成为 table
object 的一部分,成为thead
.
第二种方法是创建 Styler Subclass object 并修改 jinja
模板。
templates/myhtml.tpl
:
{% extends "html_table.tpl" %}
{% block table %}
<div class="wrap">
{% if table_title %}
<h1 class="table-title">{{ table_title}}</h1>
{% endif %}
{% if table_header %}
<h2 class="table-header">{{ table_header }}</h2>
{% endif %}
{{ super() }}
</div>
{% endblock table %}
templates/mystyles.tpl
:
{% extends "html_style.tpl" %}
{% block style %}
{{ super() }}
<style>
.wrap {
display: inline-block;
}
.table-title {
color: red;
font-size: 15px !important;
font-style: italic;
font-weight: bold;
text-align: center;
margin: 0 2px !important;
}
.table-header {
text-align: center;
font-size: 13px !important;
margin: 0 2px !important;
}
</style>
{% endblock style %}
title = 'Outputs'
header = 'header'
# Build Styler Subclass from templates
MyStyler = Styler.from_custom_template(
"templates", # Folder to Search
html_table="myhtml.tpl", # HTML Template
html_style='mystyles.tpl' # CSS Template
)
# Get Styler For `df`
styler = MyStyler(df)
# Render with arguments and display
HTML(styler.render(table_title=title, table_header=header))
这种方法将一些控制从 Styler object 转移到模板,但允许更大的灵活性,因为它可以用于创建额外的 HTML 元素,这些元素目前不受支持Styler object.
环境jupyter-notebook
设置:
import pandas as pd
from IPython.display import HTML, display
from pandas.io.formats.style import Styler
class SubclassedDataFrame(pd.DataFrame):
# normal properties
_metadata = ['description']
@property
def _constructor(self):
return SubclassedDataFrame
data = {
"a": [1, 2, 3],
"b": [10, 12, 13],
"c": [230, 1, 3],
"d": [8, 12, 9]
}
df = SubclassedDataFrame(data)