modin.pandas.DataFrame.set_axis¶

DataFrame.set_axis(labels: IndexLabel, *, axis: Axis = 0, copy: bool | NoDefault = _NoDefault.no_default)[source]¶

Assign desired index to given axis.

Parameters:
  • labels (list-like, Index, MultiIndex) – The values for the new index.

  • axis ({index (0), rows(0), columns (1)}) – Axis for the function to be applied on.

  • copy (bool, default True) – To maintain compatibility with pandas, does nothing.

Return type:

DataFrame

Examples

>>> df = pd.DataFrame({
... "Videogame": ["Dark Souls", "Cruelty Squad", "Stardew Valley"],
... "Genre": ["Souls-like", "Immersive-sim", "Farming-sim"],
... "Rating": [9.5, 9.0, 8.7]})
>>> df.set_axis(['a', 'b', 'c'], axis="index") 
        Videogame          Genre  Rating
a      Dark Souls     Souls-like     9.5
b   Cruelty Squad  Immersive-sim     9.0
c  Stardew Valley    Farming-sim     8.7
Copy
>>> df.set_axis(["Name", "Sub-genre", "Rating out of 10"], axis=1) 
             Name      Sub-genre  Rating out of 10
0      Dark Souls     Souls-like               9.5
1   Cruelty Squad  Immersive-sim               9.0
2  Stardew Valley    Farming-sim               8.7
Copy
>>> columns = pd.MultiIndex.from_tuples([("Gas", "Toyota"), ("Gas", "Ford"), ("Electric", "Tesla"), ("Electric", "Nio"),])
>>> data = [[100, 300, 900, 400], [200, 500, 300, 600]]
>>> df = pd.DataFrame(columns=columns, data=data)
>>> df.set_axis([2010, 2015], axis="rows") 
        Gas      Electric
     Toyota Ford    Tesla  Nio
2010    100  300      900  400
2015    200  500      300  600
Copy