- API reference
- DataFrame
- pandas.DataF...
pandas.DataFrame.rename#
- DataFrame.rename(mapper=None,*,index=None,columns=None,axis=None,copy=None,inplace=False,level=None,errors='ignore')[source]#
Rename columns or index labels.
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.
See theuser guide for more.
- Parameters:
- mapperdict-like or function
Dict-like or function transformations to apply tothat axis’ values. Use either
mapper
andaxis
tospecify the axis to target withmapper
, orindex
andcolumns
.- indexdict-like or function
Alternative to specifying axis (
mapper,axis=0
is equivalent toindex=mapper
).- columnsdict-like or function
Alternative to specifying axis (
mapper,axis=1
is equivalent tocolumns=mapper
).- axis{0 or ‘index’, 1 or ‘columns’}, default 0
Axis to target with
mapper
. Can be either the axis name(‘index’, ‘columns’) or number (0, 1). The default is ‘index’.- 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 modify the DataFrame rather than creating a new one.If True then value of copy is ignored.
- levelint or level name, default None
In case of a MultiIndex, only rename labels in the specifiedlevel.
- errors{‘ignore’, ‘raise’}, default ‘ignore’
If ‘raise’, raise aKeyError when a dict-likemapper,index,orcolumns contains labels that are not present in the Indexbeing transformed.If ‘ignore’, existing keys will be renamed and extra keys will beignored.
- Returns:
- DataFrame or None
DataFrame with the renamed axis labels or None if
inplace=True
.
- Raises:
- KeyError
If any of the labels is not found in the selected axis and“errors=’raise’”.
See also
DataFrame.rename_axis
Set the name of the axis.
Examples
DataFrame.rename
supports two calling conventions(index=index_mapper,columns=columns_mapper,...)
(mapper,axis={'index','columns'},...)
Wehighly recommend using keyword arguments to clarify yourintent.
Rename columns using a mapping:
>>>df=pd.DataFrame({"A":[1,2,3],"B":[4,5,6]})>>>df.rename(columns={"A":"a","B":"c"}) a c0 1 41 2 52 3 6
Rename index using a mapping:
>>>df.rename(index={0:"x",1:"y",2:"z"}) A Bx 1 4y 2 5z 3 6
Cast index labels to a different type:
>>>df.indexRangeIndex(start=0, stop=3, step=1)>>>df.rename(index=str).indexIndex(['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 b0 1 41 2 52 3 6
>>>df.rename({1:2,2:4},axis='index') A B0 1 42 2 54 3 6