search
Search
Login
Math ML Join our weekly DS/ML newsletter
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
brightness_medium
share
arrow_backShare
Twitter
Facebook

Pandas DataFrame | rename_axis method

Pandas
chevron_right
Documentation
chevron_right
DataFrame
chevron_right
Data Selection and Renaming
schedule Jul 1, 2022
Last updated
local_offer PythonPandas
Tags

Pandas's DataFrame.rename_axis(~) method modifies the axis labels.

WARNING

The method rename_axis(~) does not change the row or column labels, but instead changes the axis label. If you want to change row or column labels, use DataFrame.rename(~) instead.

Parameters

1. mapper | scalar or array-like | optional

The new name assigned to the specified axis.

2. index | scalar or array-like or dict-like or function | optional

A dictionary whose keys are the index names you want to modify, and values are the new index names.

3. columns | scalar or array-like or dict-like or function | optional

A dictionary whose keys are the column names you want to modify, and values are the new column names.

4. axis | boolean or string | optional

Whether or not to rename the rows or columns:

Axis

Description

Renaming row index.

0 or "index"

Renaming column axis.

1 or "columns"

By default, axis=0.

NOTE

Opt to use index or columns over mapper and axis for readability. See examples below for clarification.

5. copy | boolean | optional

Whether or not to return a new DataFrame without modifying the source DataFrame. By default, copy=True.

6. inplace | boolean | optional

Whether or not to perform the renaming in place:

  • If True, then the source DataFrame will be directly modified, and no new DataFrame will be created.

  • If False, then a new DataFrame is created and returned.

By default, inplace=False.

Return Value

A DataFrame with a new name for one of its axes. Note that if inplace=True, then nothing is returned since the source DataFrame is directly modified.

Examples

Consider the following DataFrame:

df = pd.DataFrame({"A":[3,4], "B":[5,6]}, index=["a","b"])
df
  A B
a 3 5
b 4 6

Our df currently does not have an axis label.

Renaming the axis label

To give df an axis label:

df.rename_axis(columns=["C"])
C A B
a 3 5
b 4 6

Here, our index (i.e. row labels) are assigned the name "C".

Equivalently, we could have used the mapper and axis parameters:

df.rename_axis(mapper="C", axis=1)      # This way is not preferred.
C A B
a 3 5
b 4 6
mail
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!