Pandas DataFrame rename() Method
Example
Rename the row indexes of the DataFrame:
import pandas as pd
data = {
"age": [50, 40, 30],
"qualified":
[True, False, False]
}
idx = ["Sally", "Mary", "John"]
df =
pd.DataFrame(data, index=idx)
newdf = df.rename({"Sally": "Pete",
"Mary": "Patrick", "John": "Paula"})
print(newdf)
Try it Yourself »
Definition and Usage
The rename() method allows you to change
the row indexes, and the columns labels.
Syntax
dataframe.rename(mapper, index, columns, axis, copy,
inplace, level, errors)
Parameters
The index, columns,
axis,
copy,
inplace,
level ,
errors parameters are
keyword arguments.
| Parameter | Value | Description |
|---|---|---|
| mapper | Optional. A dictionary where the old index/label is the key and the new index/label is the value | |
| index | old and new indexes as key/value pairs | Optional. A dictionary where the old index is the key and the new index is the value |
| columns | old and new labels as key/value pairs | Optional. A dictionary where the old label is the key and the new label is the value |
| axis | 0 |
Optional, default 0. The axis to perform the renaming (important if the mapper parameter is present and index or columns are not) |
| copy | True |
Optional, default True. Whether to also copy underlying data or not |
| inplace | True |
Optional, default False. If True: the operation is done on the current DataFrame. If False: returns a copy where the operation is done. |
| level | Number Label |
Optional, specifies which level to rename when working with MultiIndex DataFrames |
| errors | 'ignore' |
Optional, default 'ignore'. Specifies whether or not to return an error if no such index/label is present in the DataFrame |
Return Value
A DataFrame with the result, or None if the inplace parameter is set to True.
This function does NOT make changes to the original DataFrame object.