Pandas: Rename Column/index Names (labels) Of DataFrame
Maybe your like
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.
Contents- Rename column/index names: rename()
- Basic usage
- Change multiple column/index names
- Update the original object: inplace
- Rename with functions or lambda expressions
- Add prefix/suffix to column names: add_prefix(), add_suffix()
- Rename all names
- set_axis()
- Update the columns/index attributes of pandas.DataFrame
- For pandas.Series
- rename()
- add_prefix(), add_suffix()
- set_axis()
- Update the index attributes of pandas.Series
The set_index() method, which sets an existing column as the index, is also available. For more information, see the following article:
- pandas: Assign existing column to the DataFrame index with set_index()
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 source: pandas_dataframe_rename.pyRename column/index names: rename()
You can use the rename() method of DataFrame to change the column/index names individually.
- pandas.DataFrame.rename — pandas 2.0.3 documentation
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 source: pandas_dataframe_rename.pyAlternatively, 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 source: pandas_dataframe_rename.pyChange 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 source: pandas_dataframe_rename.pyUpdate 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 source: pandas_dataframe_rename.pyRename 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:
- Uppercase and lowercase strings in Python (conversion and checking)
You can also apply lambda expressions.
- Lambda expressions in Python
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 source: pandas_dataframe_rename.pyIn 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 source: pandas_dataframe_rename.pyNote 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.
- pandas.DataFrame.set_axis — pandas 2.0.3 documentation
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 source: pandas_dataframe_rename.pyNote 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 source: pandas_dataframe_rename.pyBy 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.
- What’s new in 1.5.0 (September 19, 2022) — pandas 2.0.3 documentation
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 source: pandas_dataframe_rename.pyNote 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 source: pandas_dataframe_rename.pyFor 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 source: pandas_series_rename.pyrename()
- pandas.Series.rename — pandas 2.0.3 documentation
add_prefix(), add_suffix()
- pandas.Series.add_prefix — pandas 2.0.3 documentation
- pandas.Series.add_suffix — pandas 2.0.3 documentation
set_axis()
- pandas.Series.set_axis — pandas 2.0.3 documentation
Update the index attributes of pandas.Series
s.index = ['A', 'B', 'C'] print(s) # A 1 # B 2 # C 3 # dtype: int64 source: pandas_series_rename.pyTag » How To Name Index Pandas
-
Pandas.name — Pandas 1.5.0 Documentation
-
Pandas. — Pandas 1.5.0 Documentation
-
Pandas Index Column Title Or Name - Stack Overflow
-
Pandas Set Index Name To DataFrame - Spark By {Examples}
-
Pandas Rename Column And Index - DigitalOcean
-
How To Set Column As Index In Pandas DataFrame?
-
How To Get Rows/index Names In Pandas Dataframe - GeeksforGeeks
-
Python | Change Column Names And Row Indexes In Pandas ...
-
Pandas Index Explained - Towards Data Science
-
ndas.name - Apache Spark
-
Get And Set Pandas DataFrame Index Name - Delft Stack
-
Changing The Name Of A DataFrame's Index In Pandas - SkyTowner
-
How To Rename A Pandas Dataframe Index - Datagy
-
Best Way To Set Index Name In Python Pandas Dataframe - Splunktool