pandas: Rename column/index names of DataFrame
You can rename (change) column and/or index names in a pandas.DataFrame by using the rename(), add_prefix(), add_suffix(), set_axis() methods or by directly updating the columns and/or index attributes.
Similarly, you can rename index names of a pandas.Series.
The set_index() method, which sets an existing column as the index, is also available. For more information, see the following article:
The sample code in this article uses pandas version 2.0.3. The following DataFrame is used as an example.
import pandas as pd
print(pd.__version__)
# 2.0.3
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
Rename column/index names: rename()
You can use the rename() method of DataFrame to change the column/index names individually.
Basic usage
Specify the original and new names in a dict like {original_name: new_name} for the columns and/or index arguments of the rename() method.
The columns argument is used for changing column names, and the index argument is used for changing index names. If you want to change either, you should specify only one of columns or index.
A new DataFrame is returned while the original DataFrame remains unchanged.
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
Alternatively, you can use the first argument mapper and the axis argument to determine whether to target row or column names. If axis is set to 0 or 'index', it targets the row names; if it is set to 1 or 'columns', it targets the column names. With this approach, you cannot change both row and column names simultaneously.
print(df.rename({'A': 'Col_1'}, axis='columns'))
# Col_1 B C
# ONE 11 12 13
# TWO 21 22 23
# THREE 31 32 33
Change multiple column/index names
You can change multiple column/index names at once by adding elements to dict.
print(df.rename(columns={'A': 'Col_1', 'C': 'Col_3'}))
# Col_1 B Col_3
# ONE 11 12 13
# TWO 21 22 23
# THREE 31 32 33
Update the original object: inplace
By default, the original DataFrame is not changed, and a new DataFrame is returned.
Setting the inplace argument to True changes the original DataFrame. In this case, no new DataFrame is returned, and the return value is None.
df_copy = df.copy()
df_copy.rename(columns={'A': 'Col_1'}, index={'ONE': 'Row_1'}, inplace=True)
print(df_copy)
# Col_1 B C
# Row_1 11 12 13
# TWO 21 22 23
# THREE 31 32 33
Rename with functions or lambda expressions
You can also specify functions (callable objects) in the index and columns arguments of the rename() method.
For example, you can apply functions to convert strings to either upper or lower case:
print(df.rename(columns=str.lower, index=str.title))
# a b c
# One 11 12 13
# Two 21 22 23
# Three 31 32 33
You can also apply lambda expressions.
print(df.rename(columns=lambda s: s * 3, index=lambda s: s + '!!'))
# AAA BBB CCC
# ONE!! 11 12 13
# TWO!! 21 22 23
# THREE!! 31 32 33
Add prefix/suffix to column names: add_prefix(), add_suffix()
The add_prefix() and add_suffix() methods add prefixes and suffixes to column names.
- pandas.DataFrame.add_prefix — pandas 2.0.3 documentation
- pandas.DataFrame.add_suffix — pandas 2.0.3 documentation
These methods add the specified string in the argument to either the beginning or the end of column names.
print(df.add_prefix('X_'))
# X_A X_B X_C
# ONE 11 12 13
# TWO 21 22 23
# THREE 31 32 33
print(df.add_suffix('_X'))
# A_X B_X C_X
# ONE 11 12 13
# TWO 21 22 23
# THREE 31 32 33
In version 2.0.0, the axis argument was added. If set to 0 or 'index', it targets the row names. If set to 1 or 'columns', it targets the column names. If omitted, it defaults to targeting the column names, as demonstrated in the previous example.
print(df.add_prefix('X_', axis=0))
# A B C
# X_ONE 11 12 13
# X_TWO 21 22 23
# X_THREE 31 32 33
print(df.add_suffix('_X', axis='index'))
# A B C
# ONE_X 11 12 13
# TWO_X 21 22 23
# THREE_X 31 32 33
Note that in versions prior to 2.0.0, add_prefix() and add_suffix() could only rename columns. To add prefixes or suffixes to the index, you could use the rename() method with a lambda expression in the index argument, as described above.
Additionally, add_prefix() and add_suffix() do not have the inplace argument. If you want to update the original object, overwrite it like df = df.add_prefix().
Rename all names
To change all names, use the set_axis() method or directly update the columns/index attributes.
set_axis()
You can change all column/index names using the set_axis() method of DataFrame.
Specify new column/index names for the first argument labels as a list-like object, such as a list or tuple.
Set the axis argument to 0 or 'index' to update index, and to 1 or 'columns' to update columns. If omitted, the index is updated by default.
print(df.set_axis(['Row_1', 'Row_2', 'Row_3'], axis=0))
# A B C
# Row_1 11 12 13
# Row_2 21 22 23
# Row_3 31 32 33
print(df.set_axis(['Row_1', 'Row_2', 'Row_3'], axis='index'))
# A B C
# Row_1 11 12 13
# Row_2 21 22 23
# Row_3 31 32 33
print(df.set_axis(['Col_1', 'Col_2', 'Col_3'], axis=1))
# Col_1 Col_2 Col_3
# ONE 11 12 13
# TWO 21 22 23
# THREE 31 32 33
print(df.set_axis(['Col_1', 'Col_2', 'Col_3'], axis='columns'))
# Col_1 Col_2 Col_3
# ONE 11 12 13
# TWO 21 22 23
# THREE 31 32 33
print(df.set_axis(['Row_1', 'Row_2', 'Row_3']))
# A B C
# Row_1 11 12 13
# Row_2 21 22 23
# Row_3 31 32 33
Note that an error is raised if the size (number of elements) of the specified list does not match the number of rows or columns.
# print(df.set_axis(['Row_1', 'Row_2', 'Row_3', 'Row_4']))
# ValueError: Length mismatch: Expected axis has 3 elements, new values have 4 elements
By default, the original DataFrame remains unchanged, and a new DataFrame is returned. The inplace argument was deprecated in version 1.5.0. If you want to update the original DataFrame, the release notes recommend overwriting it by setting the copy argument to False.
df_copy = df.copy()
df_copy = df_copy.set_axis(['Row_1', 'Row_2', 'Row_3'], copy=False)
print(df_copy)
# A B C
# Row_1 11 12 13
# Row_2 21 22 23
# Row_3 31 32 33
Update the columns/index attributes of pandas.DataFrame
You can also directly update the columns and index attributes of DataFrame.
You can assign lists and tuples to the columns and index attributes.
df.index = ['Row_1', 'Row_2', 'Row_3']
df.columns = ['Col_1', 'Col_2', 'Col_3']
print(df)
# Col_1 Col_2 Col_3
# Row_1 11 12 13
# Row_2 21 22 23
# Row_3 31 32 33
Note that an error is raised if the size (number of elements) of the list does not match the number of rows or columns.
# df.index = ['Row_1', 'Row_2', 'Row_3', 'Row_4']
# ValueError: Length mismatch: Expected axis has 3 elements, new values have 4 elements
For pandas.Series
You can change the index name (index) of a Series using methods similar to those used for a DataFrame, as shown in the previous examples.
For example, create a Series as follows:
s = pd.Series([1, 2, 3], index=['ONE', 'TWO', 'THREE'])
print(s)
# ONE 1
# TWO 2
# THREE 3
# dtype: int64
rename()
print(s.rename({'ONE': 'a', 'THREE': 'c'}))
# a 1
# TWO 2
# c 3
# dtype: int64
print(s.rename(str.lower))
# one 1
# two 2
# three 3
# dtype: int64
add_prefix(), add_suffix()
- pandas.Series.add_prefix — pandas 2.0.3 documentation
- pandas.Series.add_suffix — pandas 2.0.3 documentation
print(s.add_prefix('X_'))
# X_ONE 1
# X_TWO 2
# X_THREE 3
# dtype: int64
print(s.add_suffix('_X'))
# ONE_X 1
# TWO_X 2
# THREE_X 3
# dtype: int64
set_axis()
print(s.set_axis(['a', 'b', 'c']))
# a 1
# b 2
# c 3
# dtype: int64
Update the index attributes of pandas.Series
s.index = ['A', 'B', 'C']
print(s)
# A 1
# B 2
# C 3
# dtype: int64