. Advertisement .
..3..
. Advertisement .
..4..
There are many ways to change the name of the row index label or the name of a DatFrame. Index.rename() and rename.axis() functions are two common methods to serve the needs.
This post will provide detailed information on these two methods for Pandas Rename Index of DataFrame. Scroll down for detail!
What Is Rename() Function In Pandas?
A DataFrame column can easily be modified with its index. The rename() method is used to rename its row, column, and index.
Syntax:
rename( index = None, inplace = False, columns = None, mapper = None, copy = True, level = None, axis = None, index = None)
Parameters:
- index, columns, and mapper: these keys show the old value and name and refer them to a new name. You can only use one parameter at once.
- inplace: The True result allows coders to make any change to the original DataFrame.
- copy: The True result means underlying data copies.
- level: This parameter specifies the level for multi-level index DataFrames.
- axis: for string or int value. The columns value will be 1/’columns’ and that of rows is 0/’row’.
Example:
import pandas as pd
df = pd.DataFrame({'A': [11, 21, 31],
'B': [12, 22, 32],
'C': [13, 23, 33]},
index=['ONE', 'TWO', 'THREE'])
print(df)
# A B C
# ONE 11 12 13
# TWO 21 22 23
# THREE 31 32 33
How To Rename Index In DataFrame?
Method 1: Use rename() Function
This panda.DataFrame method is for individual index and column names. It specifies both original and new names in dict to columns/ index parameters.
However, it is impossible to change both at once. Specify either index or columns instead. Subsequently, the result will return a new DataFrame without changing the original one.
df_new = df.rename(columns={'A': 'Col_1'}, index={'ONE': 'Row_1'})
print(df_new)
# Col_1 B C
# Row_1 11 12 13
# TWO 21 22 23
# THREE 31 32 33
print(df)
# A B C
# ONE 11 12 13
# TWO 21 22 23
# THREE 31 32 33
Method 2: Use rename_axis() Method
DataFrame.rename_axis() is normally used to rename or add the column index. If your DataFrame has no index name, utilize this one to add a label first.
It is worth noting that the method will add an index and return a new DataFrame by default. So call the inplace=False function to update this existing one.
# Rename row with index
df1 = df.rename_axis('Index')
print(df1)
Output:
Courses Fee Duration Discount
Index
r1 Spark 20000 30days 1000
r2 PySpark 25000 40days 2300
r3 Python 22000 35days 1200
r4 pandas 30000 50days 2000
Now, it’s time to change the name of the row Index from the Index label.
# Rename Index
df2 = df.rename_axis('New_Index')
Add an Index column name with the following code:
df2=df2.rename_axis('Attributes', axis='columns')
print(df2)
Output:
Attributes Courses Fee Duration Discount
New_Index
r1 Spark 20000 30days 1000
r2 PySpark 25000 40days 2300
r3 Python 22000 35days 1200
r4 pandas 30000 50days 2000
How To Rename A DataFrame With Multiple Index
It is tricky to work with a Pandas multi-index DataFrame. Yet, renaming doesn’t cause that difficulty. The steps are similar to the above.
You will assign a value list directly to the .names attribute. Then, run the command as follows:
import pandas as pd
df = pd.DataFrame.from_dict(
{
'Year': [2018, 2019, 2020, 2021],
'Carl': [1000, 2300, 1900, 3400],
'Jane': [1500, 1700, 1300, 800],
'Melissa': [800, 2300, None, 2300]
}
).set_index(['Year', 'Carl'])
print(df)
Output:
Jane Melissa
Year Carl
2018 1000 1500 800.0
2019 2300 1700 2300.0
2020 1900 1300 NaN
2021 3400 800 2300.0
Call for the .name attribute to view the multi-index DataFrame’s names:
print(df.index.names)
# Returns: ['Year', 'Carl']
Let’s see the results if you rename ‘Average Sales’ and ‘Time Period’:
df.index.names = ['Time Period', 'Average Sales']
print(df)
Output:
Jane Melissa
Time Period Average Sales
2018 1000 1500 800.0
2019 2300 1700 2300.0
2020 1900 1300 NaN
2021 3400 800 2300.0
Conclusion
This tutorial has introduced you to two methods of Pandas Rename Index of DataFrame. For either single-index or multi-index DataFrames, there will be different ways to work out. So, prefer to this blog again to get the answers.
Leave a comment