Rename columns or index labels.
Function / dict values must be unique (1-to-1). Labels not contained in a dict / Series will be left as-is. Extra labels listed donât throw an error.
See the user guide for more.
Dict-like or function transformations to apply to that axisâ values. Use either mapper
and axis
to specify the axis to target with mapper
, or index
and columns
.
Alternative to specifying axis (mapper, axis=0
is equivalent to index=mapper
).
Alternative to specifying axis (mapper, axis=1
is equivalent to columns=mapper
).
Axis to target with mapper
. Can be either the axis name (âindexâ, âcolumnsâ) or number (0, 1). The default is âindexâ.
Also copy underlying data.
Note
The copy keyword will change behavior in pandas 3.0. Copy-on-Write will be enabled by default, which means that all methods with a copy keyword will use a lazy copy mechanism to defer the copy and ignore the copy keyword. The copy keyword will be removed in a future version of pandas.
You can already get the future behavior and improvements through enabling copy on write pd.options.mode.copy_on_write = True
Deprecated since version 3.0.0.
Whether to modify the DataFrame rather than creating a new one. If True then value of copy is ignored.
In case of a MultiIndex, only rename labels in the specified level.
If âraiseâ, raise a KeyError when a dict-like mapper, index, or columns contains labels that are not present in the Index being transformed. If âignoreâ, existing keys will be renamed and extra keys will be ignored.
DataFrame with the renamed axis labels or None if inplace=True
.
If any of the labels is not found in the selected axis and âerrors=âraiseââ.
Examples
DataFrame.rename
supports two calling conventions
(index=index_mapper, columns=columns_mapper, ...)
(mapper, axis={'index', 'columns'}, ...)
We highly recommend using keyword arguments to clarify your intent.
Rename columns using a mapping:
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) >>> df.rename(columns={"A": "a", "B": "c"}) a c 0 1 4 1 2 5 2 3 6
Rename index using a mapping:
>>> df.rename(index={0: "x", 1: "y", 2: "z"}) A B x 1 4 y 2 5 z 3 6
Cast index labels to a different type:
>>> df.index RangeIndex(start=0, stop=3, step=1) >>> df.rename(index=str).index Index(['0', '1', '2'], dtype='object')
>>> df.rename(columns={"A": "a", "B": "b", "C": "c"}, errors="raise") Traceback (most recent call last): KeyError: ['C'] not found in axis
Using axis-style parameters:
>>> df.rename(str.lower, axis="columns") a b 0 1 4 1 2 5 2 3 6
>>> df.rename({1: 2, 2: 4}, axis="index") A B 0 1 4 2 2 5 4 3 6
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4