pandas.io.formats.style.Styler.concat#

Styler.concat(other)[源代码]#

追加另一个样式器以将输出合并到单个表中。

1.5.0 新版功能.

参数
other造型师

已设置样式和格式的另一个Styler对象。此Styler的数据必须与原始数据具有相同的列,并且索引级别的数量也必须相同才能正确呈现。

退货
self造型师

注意事项

此方法的目的是使用可能有用但可能不符合原始结构的其他度量来扩展现有的样式化数据帧。例如,添加小计行,或显示均值、方差或计数等指标。

应用的样式使用 applyapplymapapply_indexapplymap_index ,和应用的格式 formatformat_index 都会被保存下来。

警告

只有输出方法 to_htmlto_stringto_latex 当前使用串联样式器。

其他输出方法,包括 to_excel请勿 使用串联样式器。

应注意以下几点:

  • table_stylestable_attributescaptionuuid 都继承自原始的Styler,而不是 other

  • 隐藏列和隐藏索引级别将从原始样式器继承

一种常见用例是将用户定义的函数与 DataFrame.agg 或通过以下方式提供描述的统计数据 DataFrame.describe 。请参见示例。

示例

一种常见的用例是添加总计行,或者通过 DataFrame.agg

>>> df = DataFrame([[4, 6], [1, 9], [3, 4], [5, 5], [9,6]],
...                columns=["Mike", "Jim"],
...                index=["Mon", "Tue", "Wed", "Thurs", "Fri"])
>>> styler = df.style.concat(df.agg(["sum"]).style)  
../../_images/footer_simple.png

由于连接的对象是Styler,因此可以使用现有功能来有条件地格式化它和原始对象。

>>> descriptors = df.agg(["sum", "mean", lambda s: s.dtype])
>>> descriptors.index = ["Total", "Average", "dtype"]
>>> other = (descriptors.style
...          .highlight_max(axis=1, subset=(["Total", "Average"], slice(None)))
...          .format(subset=("Average", slice(None)), precision=2, decimal=",")
...          .applymap(lambda v: "font-weight: bold;"))
>>> styler = (df.style
...             .highlight_max(color="salmon")
...             .set_table_styles([{"selector": ".foot_row0",
...                                 "props": "border-top: 1px solid black;"}]))
>>> styler.concat(other)  
../../_images/footer_extended.png

什么时候 other 具有比原始Styler更少的索引级别,因此可以在 other ,具有占位符级别。

>>> df = DataFrame([[1], [2]], index=pd.MultiIndex.from_product([[0], [1, 2]]))
>>> descriptors = df.agg(["sum"])
>>> descriptors.index = pd.MultiIndex.from_product([[""], descriptors.index])
>>> df.style.concat(descriptors.style)