Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

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

Styler.concat(other)[source]#

Append another Styler to combine the output into a single table.

Added in version 1.5.0.

Parameters:
otherStyler

The other Styler object which has already been styled and formatted. Thedata for this Styler must have the same columns as the original, and thenumber of index levels must also be the same to render correctly.

Returns:
Styler

Notes

The purpose of this method is to extend existing styled dataframes with othermetrics 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 theapply,map,apply_indexandmap_index, and formatting applied withformat andformat_index will be preserved.

Warning

Only the output methodsto_html,to_string andto_latexcurrently work with concatenated Stylers.

Other output methods, includingto_excel,do not work withconcatenated Stylers.

The following should be noted:

  • table_styles,table_attributes,caption anduuid are allinherited from the original Styler and notother.

  • hidden columns and hidden index levels will be inherited from theoriginal Styler

  • css will be inherited from the original Styler, and the value ofkeysdata,row_heading androw will be prepended withfoot0_. If more concats are chained, their styles will be prependedwithfoot1_, ‘’foot_2’’, etc., and if a concatenated style haveanother concatanated style, the second style will be prepended withfoot{parent}_foot{child}_.

A common use case is to concatenate user defined functions withDataFrame.agg or with described statistics viaDataFrame.describe.See examples.

Examples

A common use case is adding totals rows, or otherwise, via methods calculatedinDataFrame.agg.

>>>df=pd.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

Since the concatenated object is a Styler the existing functionality can beused to conditionally format it as well as the original.

>>>descriptors=df.agg(["sum","mean",lambdas: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=",")....map(lambdav:"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

Whenother has fewer index levels than the original Styler it is possibleto extend the index inother, with placeholder levels.

>>>df=pd.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)

[8]ページ先頭

©2009-2025 Movatter.jp