pandas.DataFrame.set_axis

DataFrame.set_axis(labels, axis=0, inplace=None)[source]

Assign desired index to given axis

Parameters:

labels: list-like or Index

The values for the new index

axis : int or string, default 0

inplace : boolean, default None

Whether to return a new NDFrame instance.

WARNING: inplace=None currently falls back to to True, but in a future version, will default to False. Use inplace=True explicitly rather than relying on the default.

.. versionadded:: 0.21.0

The signature is make consistent to the rest of the API. Previously, the “axis” and “labels” arguments were respectively the first and second positional arguments.

Returns:

renamed : NDFrame or None

An object of same type as caller if inplace=False, None otherwise.

See also

pandas.NDFrame.rename

Examples

>>> s = pd.Series([1, 2, 3])
>>> s
0    1
1    2
2    3
dtype: int64
>>> s.set_axis(['a', 'b', 'c'], axis=0, inplace=False)
a    1
b    2
c    3
dtype: int64
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
>>> df.set_axis(['a', 'b', 'c'], axis=0, inplace=False)
   A  B
a  1  4
b  2  5
c  3  6
>>> df.set_axis(['I', 'II'], axis=1, inplace=False)
   I  II
0  1   4
1  2   5
2  3   6
>>> df.set_axis(['i', 'ii'], axis=1, inplace=True)
>>> df
   i  ii
0  1   4
1  2   5
2  3   6
Scroll To Top