- API reference
- DataFrame
- pandas.DataF...
pandas.DataFrame.set_axis#
- DataFrame.set_axis(labels,*,axis=0,copy=None)[source]#
Assign desired index to given axis.
Indexes for column or row labels can be changed by assigninga list-like or Index.
- Parameters:
- labelslist-like, Index
The values for the new index.
- axis{0 or ‘index’, 1 or ‘columns’}, default 0
The axis to update. The value 0 identifies the rows. ForSeriesthis parameter is unused and defaults to 0.
- copybool, default True
Whether to make a copy of the 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
- Returns:
- DataFrame
An object of type DataFrame.
See also
DataFrame.rename_axis
Alter the name of the index or columns.
Examples
>>>df=pd.DataFrame({"A":[1,2,3],"B":[4,5,6]})
Change the row labels.
>>>df.set_axis(['a','b','c'],axis='index') A Ba 1 4b 2 5c 3 6
Change the column labels.
>>>df.set_axis(['I','II'],axis='columns') I II0 1 41 2 52 3 6