Table of Contents
Pandas is a popular data manipulation library in Python that allows for efficient and effective data analysis. One common task in data analysis is renaming columns in a dataset. This can be easily achieved in Pandas using the “rename” function. By specifying the original column name and the desired new name, the columns can be renamed in a quick and straightforward manner. Additionally, Pandas offers various options for renaming columns, such as renaming multiple columns at once or using a dictionary to map old names to new names. This feature of Pandas makes it a powerful tool for managing and organizing data in an efficient and customizable manner.
Rename Columns in Pandas (With Examples)
You can use one of the following three methods to rename columns in a pandas DataFrame:
Method 1: Rename Specific Columns
df.rename(columns = {'old_col1':'new_col1', 'old_col2':'new_col2'}, inplace = True)
Method 2: Rename All Columns
df.columns = ['new_col1', 'new_col2', 'new_col3', 'new_col4']
Method 3: Replace Specific Characters in Columns
df.columns = df.columns.str.replace('old_char', 'new_char')
The following examples show how to use each of these methods in practice.
Method 1: Rename Specific Columns
The following code shows how to rename specific columns in a pandas DataFrame:
import pandas as pd #define DataFrame df = pd.DataFrame({'team':['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'], 'points': [25, 12, 15, 14, 19, 23, 25, 29], 'assists': [5, 7, 7, 9, 12, 9, 9, 4], 'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]}) #list column names list(df) ['team', 'points', 'assists', 'rebounds'] #rename specific column names df.rename(columns = {'team':'team_name', 'points':'points_scored'}, inplace = True) #view updated list of column names list(df) ['team_name', 'points_scored', 'assists', 'rebounds']
Notice that the ‘team’ and ‘points’ columns were renamed while all other column names remained the same.
Method 2: Rename All Columns
The following code shows how to rename all columns in a pandas DataFrame:
import pandas as pd #define DataFrame df = pd.DataFrame({'team':['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'], 'points': [25, 12, 15, 14, 19, 23, 25, 29], 'assists': [5, 7, 7, 9, 12, 9, 9, 4], 'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]}) #list column names list(df) ['team', 'points', 'assists', 'rebounds'] #rename all column names df.columns = ['_team', '_points', '_assists', '_rebounds'] #view updated list of column names list(df) ['_team', '_points', '_assists', '_rebounds']
Note that it’s faster to use this method when you want to rename most or all of the column names in the DataFrame.
Method 3: Replace Specific Characters in Columns
The following code shows how to replace a specific character in each column name:
import pandas as pd #define DataFrame df = pd.DataFrame({'$team':['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'], '$points': [25, 12, 15, 14, 19, 23, 25, 29], '$assists': [5, 7, 7, 9, 12, 9, 9, 4], '$rebounds': [11, 8, 10, 6, 6, 5, 9, 12]}) #list column names list(df) ['team', 'points', 'assists', 'rebounds'] #rename $ with blank in every column name df.columns = df.columns.str.replace('$', '') #view updated list of column names list(df) ['team', 'points', 'assists', 'rebounds']
Notice that this method allowed us to quickly remove the ‘$’ from each column name.
The following tutorials explain how to perform other common operations in pandas:
Cite this article
stats writer (2024). How can I rename columns in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-rename-columns-in-pandas/
stats writer. "How can I rename columns in Pandas?." PSYCHOLOGICAL SCALES, 12 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-rename-columns-in-pandas/.
stats writer. "How can I rename columns in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-rename-columns-in-pandas/.
stats writer (2024) 'How can I rename columns in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-rename-columns-in-pandas/.
[1] stats writer, "How can I rename columns in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I rename columns in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
