Pandas df.rename() function
df.rename() is a function in Pandas used to rename the columns or index of a DataFrame.
In data analysis, clear column names and indexes are very important for code readability and maintainability.rename() allows you to flexibly modify column names and index names, making data more standardized and easier to understand. This is especially useful during data import, cleaning, and report generation.
Basic syntax and parameters
rename() is a member function of DataFrame, called via the dot operator . to call it.
Syntax format
DataFrame.rename(mapper=None, index=None, columns=None, axis=None, inplace=False, errors='ignore')
Parameter Description
| Parameters | Type | Required | Description | Default value |
|---|---|---|---|---|
| mapper | dict or function | Optional | The mapping rule used to rename an axis (row index or column name). It can be a dictionary or a function. Used together with axis the parameter. |
None |
| index | dict or function | Optional | Directly specify the renaming rule for the row index. Takes precedence over mapper and axis。 |
None |
| columns | dict or function | Optional | Directly specify the renaming rule for column names. Takes precedence over mapper and axis。 |
None |
| axis | int or str | Optional | Specify the axis to rename.0 or 'index' represents the row index;1 or 'columns' represents column names. |
None |
| inplace | bool | Optional | If it is True, modify the original DataFrame directly without returning a new object; if False, a new DataFrame is returned and the original data is unchanged. |
False |
| errors | str | Optional | Controls error handling.'raise' means raise an exception when a corresponding key is not found;'ignore' means ignore keys that do not exist. |
'ignore' |
Return value description
- Returns a new DataFrame (if
inplace=False), orNone(ifinplace=True)。 - returns a DataFrame in which the column names or index have been renamed.
Example
Let us thoroughly master rename() through a series of examples from simple to complex.
Example 1: Renaming column names
Use columns The parameter can conveniently rename column names.
Example
import
pandas
as
pd
Create a DataFrame
data
=
{
'name'
:
[
'Zhang San'
,
'Li Si'
,
'Wang Wu'
]
,
'age'
:
[
25
,
30
,
35
]
,
'salary'
:
[
5000
,
6000
,
7000
]
}
df
=
pd.
DataFrame
(
data
)
(
"Original data:"
)
(
df
)
(
"="
50
)
Rename column names using a dictionary
df_renamed
=
df.
rename
(
columns
=
{
'name'
:
'Name'
,
'age'
:
'Age'
,
'salary'
:
'Salary'
}
)
(
"Data after renaming columns:"
)
(
df_renamed
)
Expected output:
原始数据:
name age salary
0 张三 25 5000
1 李四 30 6000
2 王五 35 7000
==================================================
Data after renaming column names:
姓名 年龄 薪资
0 张三 25 5000
1 李四 30 6000
2 王五 35 7000
Code analysis:
- The original column names are in English; we use a dictionary to rename them to Chinese names.
- Use
columnsThe parameter only affects column names, not the row index. - This is a common data standardization operation.
Example 2: Renaming the row index
Use index The parameter can rename the row index.
Example
import
pandas
as
pd
Create a DataFrame with the default index
data
=
{
'fruit'
:
[
'Apple'
,
'Banana'
,
'Orange'
]
,
'quantity'
:
[
10
,
20
,
15
]
}
df
=
pd.
DataFrame
(
data
)
(
"Original data (default index):"
)
(
df
)
(
"="
50
)
Rename the row index using a dictionary
df_renamed
=
df.
rename
(
index
=
{
0
:
'First line'
,
1
:
'Second line'
,
2
:
'Third line'
}
)
(
"Data after renaming the row index:"
)
(
df_renamed
)
Expected output:
原始数据(默认索引):
水果 数量
0 苹果 10
1 香蕉 20
2 橙子 15
==================================================
Rename data after row index:
水果 数量
第一行 苹果 10
第二行 香蕉 20
第三行 橙子 15
Code analysis:
- The original row index consists of numbers like 0, 1, 2.
- Use
indexThe parameter can rename them to more meaningful names. - This is especially useful when generating reports.
Common Pandas functions