Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.DataFrame.rename_axis#

DataFrame.rename_axis(mapper=<no_default>,*,index=<no_default>,columns=<no_default>,axis=0,copy=None,inplace=False)[source]#

Set the name of the axis for the index or columns.

Parameters:
mapperscalar, list-like, optional

Value to set the axis name attribute.

index, columnsscalar, list-like, dict-like or function, optional

A scalar, list-like, dict-like or functions transformations toapply to that axis’ values.Note that thecolumns parameter is not allowed if theobject is a Series. This parameter only apply for DataFrametype objects.

Use eithermapper andaxis tospecify the axis to target withmapper, orindexand/orcolumns.

axis{0 or ‘index’, 1 or ‘columns’}, default 0

The axis to rename. ForSeries this parameter is unused and defaults to 0.

copybool, default None

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 writepd.options.mode.copy_on_write=True

inplacebool, default False

Modifies the object directly, instead of creating a new Seriesor DataFrame.

Returns:
Series, DataFrame, or None

The same type as the caller or None ifinplace=True.

See also

Series.rename

Alter Series index labels or name.

DataFrame.rename

Alter DataFrame index labels or name.

Index.rename

Set new names on index.

Notes

DataFrame.rename_axis supports two calling conventions

  • (index=index_mapper,columns=columns_mapper,...)

  • (mapper,axis={'index','columns'},...)

The first calling convention will only modify the names ofthe index and/or the names of the Index object that is the columns.In this case, the parametercopy is ignored.

The second calling convention will modify the names of thecorresponding index if mapper is a list or a scalar.However, if mapper is dict-like or a function, it will use thedeprecated behavior of modifying the axislabels.

Wehighly recommend using keyword arguments to clarify yourintent.

Examples

Series

>>>s=pd.Series(["dog","cat","monkey"])>>>s0       dog1       cat2    monkeydtype: object>>>s.rename_axis("animal")animal0    dog1    cat2    monkeydtype: object

DataFrame

>>>df=pd.DataFrame({"num_legs":[4,4,2],..."num_arms":[0,0,2]},...["dog","cat","monkey"])>>>df        num_legs  num_armsdog            4         0cat            4         0monkey         2         2>>>df=df.rename_axis("animal")>>>df        num_legs  num_armsanimaldog            4         0cat            4         0monkey         2         2>>>df=df.rename_axis("limbs",axis="columns")>>>dflimbs   num_legs  num_armsanimaldog            4         0cat            4         0monkey         2         2

MultiIndex

>>>df.index=pd.MultiIndex.from_product([['mammal'],...['dog','cat','monkey']],...names=['type','name'])>>>dflimbs          num_legs  num_armstype   namemammal dog            4         0       cat            4         0       monkey         2         2
>>>df.rename_axis(index={'type':'class'})limbs          num_legs  num_armsclass  namemammal dog            4         0       cat            4         0       monkey         2         2
>>>df.rename_axis(columns=str.upper)LIMBS          num_legs  num_armstype   namemammal dog            4         0       cat            4         0       monkey         2         2

[8]ページ先頭

©2009-2025 Movatter.jp