w3resource

Pandas DataFrame: rename() function

DataFrame - rename() function

The rename() function is used to alter axes labels.

Syntax:

DataFrame.rename(self, mapper=None, index=None, columns=None, axis=None, copy=True, inplace=False, level=None, errors='ignore')

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.

Parameters:

Name Description Type / Default Value Required / Optional
mapper  Dict-like or functions transformations to apply to that axis’ values. Use either mapper and axis to specify the axis to target with mapper, or index and columns. dict-like or function Required
index   Alternative to specifying axis (mapper, axis=0 is equivalent to index=mapper). dict-like or function Required
columns    Alternative to specifying axis (mapper, axis=1 is equivalent to columns=mapper). dict-like or function Required
axis    Axis to target with mapper. Can be either the axis name (‘index’, ‘columns’) or number (0, 1). The default is ‘index’. int or str Required
copy    Also copy underlying data bool
Default Value: True
Required
inplace  Whether to return a new DataFrame. If True then value of copy is ignored. bool
Default Value: False
Required
level  In case of a MultiIndex, only rename labels in the specified level. int or level name
Default Value: None
Required
errors  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. {‘ignore’, ‘raise’}
Default Value: ‘ignore’
Required

Returns: DataFrame
DataFrame with the renamed axis labels.

Raises: KeyError
If any of the labels is not found in the selected axis and “errors=’raise’”.

Example:


Download the Pandas DataFrame Notebooks from here.

Previous: DataFrame - reindex_like() function
Next: DataFrame - rename_axis() function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/pandas/dataframe/dataframe-rename.php