pandas.io.formats.style.Styler.concat¶
- Styler.concat(other)[source]¶
Append another Styler to combine the output into a single table.
New in version 1.5.0.
- Parameters
- otherStyler
The other Styler object which has already been styled and formatted. The data for this Styler must have the same columns as the original, and the number of index levels must also be the same to render correctly.
- Returns
- selfStyler
Notes
The purpose of this method is to extend existing styled dataframes with other metrics that may be useful but may not conform to the original’s structure. For example adding a sub total row, or displaying metrics such as means, variance or counts.
Styles that are applied using the
apply
,applymap
,apply_index
andapplymap_index
, and formatting applied withformat
andformat_index
will be preserved.Warning
Only the output methods
to_html
,to_string
andto_latex
currently work with concatenated Stylers.Other output methods, including
to_excel
, do not work with concatenated Stylers.The following should be noted:
table_styles
,table_attributes
,caption
anduuid
are all inherited from the original Styler and notother
.hidden columns and hidden index levels will be inherited from the original Styler
A common use case is to concatenate user defined functions with
DataFrame.agg
or with described statistics viaDataFrame.describe
. See examples.Examples
A common use case is adding totals rows, or otherwise, via methods calculated in
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)
Since the concatenated object is a Styler the existing functionality can be used to conditionally format it as well as the original.
>>> 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)
When
other
has fewer index levels than the original Styler it is possible to extend the index inother
, with placeholder levels.>>> 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)