pandas 1.4.2

ParametersRaisesReturns
_rename(self: 'NDFrameT', mapper: 'Renamer | None' = None, *, index: 'Renamer | None' = None, columns: 'Renamer | None' = None, axis: 'Axis | None' = None, copy: 'bool_t' = True, inplace: 'bool_t' = False, level: 'Level | None' = None, errors: 'str' = 'ignore') -> 'NDFrameT | None'

Parameters

%(axes)s : scalar, list-like, dict-like or function, optional

Scalar or list-like will alter the Series.name attribute, and raise on DataFrame. dict-like or functions are transformations to apply to that axis' values

copy : bool, default True

Also copy underlying data.

inplace : bool, default False

Whether to return a new {klass}. If True then value of copy is ignored.

level : int or level name, default None

In case of a MultiIndex, only rename labels in the specified level.

errors : {'ignore', 'raise'}, default 'ignore'

If 'raise', raise a :None:None:`KeyError` when a dict-like mapper , :None:None:`index`, or :None:None:`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.

Raises

KeyError

If any of the labels is not found in the selected axis and "errors='raise'".

Returns

renamed : {klass} (new object)

Alter axes input function or functions. 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. Alternatively, change Series.name with a scalar value (Series only).

See Also

NDFrame.rename_axis

Examples

This example is valid syntax, but we were not able to check execution
>>> s = pd.Series([1, 2, 3])
... s 0 1 1 2 2 3 dtype: int64
This example is valid syntax, but we were not able to check execution
>>> s.rename("my_name") # scalar, changes Series.name
0    1
1    2
2    3
Name: my_name, dtype: int64
This example is valid syntax, but we were not able to check execution
>>> s.rename(lambda x: x ** 2)  # function, changes labels
0    1
1    2
4    3
dtype: int64
This example is valid syntax, but we were not able to check execution
>>> s.rename({1: 3, 2: 5})  # mapping, changes labels
0    1
3    2
5    3
dtype: int64

Since DataFrame doesn't have a .name attribute, only mapping-type arguments are allowed.

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
... df.rename(2) Traceback (most recent call last): ... TypeError: 'int' object is not callable

DataFrame.rename supports two calling conventions

We highly recommend using keyword arguments to clarify your intent.

This example is valid syntax, but we were not able to check execution
>>> df.rename(index=str, columns={"A": "a", "B": "c"})
   a  c
0  1  4
1  2  5
2  3  6
This example is valid syntax, but we were not able to check execution
>>> df.rename(index=str, columns={"A": "a", "C": "c"})
   a  B
0  1  4
1  2  5
2  3  6

Using axis-style parameters

This example is valid syntax, but we were not able to check execution
>>> df.rename(str.lower, axis='columns')
   a  b
0  1  4
1  2  5
2  3  6
This example is valid syntax, but we were not able to check execution
>>> df.rename({1: 2, 2: 4}, axis='index')
   A  B
0  1  4
2  2  5
4  3  6

See the user guide <basics.rename> for more.

See :

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/generic.py#985
type: <class 'function'>
Commit: