Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.io.formats.style.Styler.relabel_index#

Styler.relabel_index(labels,axis=0,level=None)[source]#

Relabel the index, or column header, keys to display a set of specified values.

Added in version 1.5.0.

Parameters:
labelslist-like or Index

New labels to display. Must have same length as the underlying values nothidden.

axis{“index”, 0, “columns”, 1}

Apply to the index or columns.

levelint, str, list, optional

The level(s) over which to apply the new labels. IfNone will applyto all levels of an Index or MultiIndex which are not hidden.

Returns:
Styler

See also

Styler.format_index

Format the text display value of index or column headers.

Styler.hide

Hide the index, column headers, or specified data from display.

Notes

As part of Styler, this method allows the display of an index to becompletely user-specified without affecting the underlying DataFrame data,index, or column headers. This means that the flexibility of indexing ismaintained whilst the final display is customisable.

Since Styler is designed to be progressively constructed with method chaining,this method is adapted to react to thecurrently specified hidden elements.This is useful because it means one does not have to specify all the newlabels if the majority of an index, or column headers, have already been hidden.The following produce equivalent display (note the length oflabels ineach case).

# relabel first, then hidedf=pd.DataFrame({"col":["a","b","c"]})df.style.relabel_index(["A","B","C"]).hide([0,1])# hide first, then relabeldf=pd.DataFrame({"col":["a","b","c"]})df.style.hide([0,1]).relabel_index(["C"])

This method should be used, rather thanStyler.format_index(), in one ofthe following cases (see examples):

  • A specified set of labels are required which are not a function of theunderlying index keys.

  • The function of the underlying index keys requires a counter variable,such as those available upon enumeration.

Examples

Basic use

>>>df=pd.DataFrame({"col":["a","b","c"]})>>>df.style.relabel_index(["A","B","C"])     colA      aB      bC      c

Chaining with pre-hidden elements

>>>df.style.hide([0,1]).relabel_index(["C"])     colC      c

Using a MultiIndex

>>>midx=pd.MultiIndex.from_product([[0,1],[0,1],[0,1]])>>>df=pd.DataFrame({"col":list(range(8))},index=midx)>>>styler=df.style          col0  0  0     0      1     1   1  0     2      1     31  0  0     4      1     5   1  0     6      1     7>>>styler.hide((midx.get_level_values(0)==0)|(midx.get_level_values(1)==0))...>>>styler.hide(level=[0,1])>>>styler.relabel_index(["binary6","binary7"])          colbinary6     6binary7     7

We can also achieve the above by indexing first and then re-labeling

>>>styler=df.loc[[(1,1,0),(1,1,1)]].style>>>styler.hide(level=[0,1]).relabel_index(["binary6","binary7"])...          colbinary6     6binary7     7

Defining a formatting function which uses an enumeration counter. Also notethat the value of the index key is passed in the case of string labels so itcan also be inserted into the label, using curly brackets (or double curlybrackets if the string if pre-formatted),

>>>df=pd.DataFrame({"samples":np.random.rand(10)})>>>styler=df.loc[np.random.randint(0,10,3)].style>>>styler.relabel_index([f"sample{i+1} ({{}})"foriinrange(3)])...                 samplessample1 (5)     0.315811sample2 (0)     0.495941sample3 (2)     0.067946

[8]ページ先頭

©2009-2025 Movatter.jp