- API reference
- Series
- pandas.Series.rename
pandas.Series.rename#
- Series.rename(index=None,*,axis=None,copy=None,inplace=False,level=None,errors='ignore')[source]#
Alter Series index labels or name.
Function / dict values must be unique (1-to-1). Labels not contained ina dict / Series will be left as-is. Extra labels listed don’t throw anerror.
Alternatively, change
Series.name
with a scalar value.See theuser guide for more.
- Parameters:
- indexscalar, hashable sequence, dict-like or function optional
Functions or dict-like are transformations to apply tothe index.Scalar or hashable sequence-like will alter the
Series.name
attribute.- axis{0 or ‘index’}
Unused. Parameter needed for compatibility with DataFrame.
- copybool, default True
Also copy underlying data.
Note
Thecopy keyword will change behavior in pandas 3.0.Copy-on-Writewill be enabled by default, which means that all methods with acopy keyword will use a lazy copy mechanism to defer the copy andignore thecopy keyword. Thecopy keyword will be removed in afuture version of pandas.
You can already get the future behavior and improvements throughenabling copy on write
pd.options.mode.copy_on_write=True
- inplacebool, default False
Whether to return a new Series. If True the value of copy is ignored.
- levelint or level name, default None
In case of MultiIndex, only rename labels in the specified level.
- errors{‘ignore’, ‘raise’}, default ‘ignore’
If ‘raise’, raiseKeyError when adict-like mapper orindex contains labels that are not present in the index being transformed.If ‘ignore’, existing keys will be renamed and extra keys will be ignored.
- Returns:
- Series or None
Series with index labels or name altered or None if
inplace=True
.
See also
DataFrame.rename
Corresponding DataFrame method.
Series.rename_axis
Set the name of the axis.
Examples
>>>s=pd.Series([1,2,3])>>>s0 11 22 3dtype: int64>>>s.rename("my_name")# scalar, changes Series.name0 11 22 3Name: my_name, dtype: int64>>>s.rename(lambdax:x**2)# function, changes labels0 11 24 3dtype: int64>>>s.rename({1:3,2:5})# mapping, changes labels0 13 25 3dtype: int64