Rename Specific Columns

Use the df.rename() function and refer the columns to be renamed. Not all the columns have to be renamed:

Copydf = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'})

# Or rename the existing DataFrame (rather than creating a copy) 
df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}, inplace=True)

Minimal Code Example

Copydf = pd.DataFrame('x', index=range(3), columns=list('abcde'))
df

   a  b  c  d  e
0  x  x  x  x  x
1  x  x  x  x  x
2  x  x  x  x  x

The following methods all work and produce the same output:

Copydf2 = df.rename({'a': 'X', 'b': 'Y'}, axis=1)
df2 = df.rename({'a': 'X', 'b': 'Y'}, axis='columns')
df2 = df.rename(columns={'a': 'X', 'b': 'Y'}) 

df2

   X  Y  c  d  e
0  x  x  x  x  x
1  x  x  x  x  x
2  x  x  x  x  x

Remember to assign the result back, as the modification is not-inplace. Alternatively, specify inplace=True:

Copydf.rename({'a': 'X', 'b': 'Y'}, axis=1, inplace=True)
df

   X  Y  c  d  e
0  x  x  x  x  x
1  x  x  x  x  x
2  x  x  x  x  x
 

You can specify errors='raise' to raise errors if an invalid column-to-rename is specified.


Reassign Column Headers

Use df.set_axis() with axis=1.

Copydf2 = df.set_axis(['V', 'W', 'X', 'Y', 'Z'], axis=1)
df2

   V  W  X  Y  Z
0  x  x  x  x  x
1  x  x  x  x  x
2  x  x  x  x  x

Headers can be assigned directly:

Copydf.columns = ['V', 'W', 'X', 'Y', 'Z']
df

   V  W  X  Y  Z
0  x  x  x  x  x
1  x  x  x  x  x
2  x  x  x  x  x
Answer from lexual on Stack Overflow
Top answer
1 of 16
4717

Rename Specific Columns

Use the df.rename() function and refer the columns to be renamed. Not all the columns have to be renamed:

Copydf = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'})

# Or rename the existing DataFrame (rather than creating a copy) 
df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}, inplace=True)

Minimal Code Example

Copydf = pd.DataFrame('x', index=range(3), columns=list('abcde'))
df

   a  b  c  d  e
0  x  x  x  x  x
1  x  x  x  x  x
2  x  x  x  x  x

The following methods all work and produce the same output:

Copydf2 = df.rename({'a': 'X', 'b': 'Y'}, axis=1)
df2 = df.rename({'a': 'X', 'b': 'Y'}, axis='columns')
df2 = df.rename(columns={'a': 'X', 'b': 'Y'}) 

df2

   X  Y  c  d  e
0  x  x  x  x  x
1  x  x  x  x  x
2  x  x  x  x  x

Remember to assign the result back, as the modification is not-inplace. Alternatively, specify inplace=True:

Copydf.rename({'a': 'X', 'b': 'Y'}, axis=1, inplace=True)
df

   X  Y  c  d  e
0  x  x  x  x  x
1  x  x  x  x  x
2  x  x  x  x  x
 

You can specify errors='raise' to raise errors if an invalid column-to-rename is specified.


Reassign Column Headers

Use df.set_axis() with axis=1.

Copydf2 = df.set_axis(['V', 'W', 'X', 'Y', 'Z'], axis=1)
df2

   V  W  X  Y  Z
0  x  x  x  x  x
1  x  x  x  x  x
2  x  x  x  x  x

Headers can be assigned directly:

Copydf.columns = ['V', 'W', 'X', 'Y', 'Z']
df

   V  W  X  Y  Z
0  x  x  x  x  x
1  x  x  x  x  x
2  x  x  x  x  x
2 of 16
2574

Just assign it to the .columns attribute:

Copy>>> df = pd.DataFrame({'$a':[1,2], '$b': [10,20]})
>>> df
   b
0   1  10
1   2  20

>>> df.columns = ['a', 'b']
>>> df
   a   b
0  1  10
1  2  20
Discussions

In pandas, when should I use column names as attributes and when to use them as indexes?
I thought Pandas was deprecating attribute-style access and wants everyone to use index-style access. Can't remember why I think that, but I think it's a good practice never to refer to attributes that aren't declared somewhere in code. If the layout comes from external data (as it does when you load a dataframe) then you should use string indexes. More on reddit.com
๐ŸŒ r/learnpython
5
4
January 1, 2022
How to add a header row above the column names in pandas
df.columns =['Name', 'Code', 'Age', 'Weight'] More on reddit.com
๐ŸŒ r/learnpython
10
0
April 12, 2023
Adding a header and a blank row before column names (Pandas)
# Create your original dataframe df_data = pd.DataFrame([['Year', 'Week', 'Value'], ['2022', '01', '300']]) # Create your headers df = pd.DataFrame(["Silly Header", '']) # Combine both pd.concat([df, df_data]) Post your code and data examples if it isn't working. More on reddit.com
๐ŸŒ r/learnpython
5
3
March 28, 2024
Re-Assign values in a specific dataframe column using .iloc[]
To give us the best chance to help you, please include any relevant code. Note. Do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide ). If you have formatting issues or want to post longer sections of code, please use Repl.it , GitHub or PasteBin . I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
๐ŸŒ r/pythonhelp
2
September 16, 2023
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ pandas โ€บ add-column-names-to-dataframe-in-pandas
Add column names to dataframe in Pandas - GeeksforGeeks
July 15, 2025 - While assign() is typically used for adding new columns to a DataFrame, you can also use it to add columns with meaningful names. This method returns a new DataFrame with the additional columns. ... Player Alias Years Points Experience 0 Amar Alpha 23 69 5 1 Barsha Bravo 25 54 3 2 Carlos Charlie 22 73 4 3 Tanmay Tango 27 70 6 4 Misbah Mike 29 74 2 ยท If you want to add a prefix or suffix to every column name, you can use the add_prefix() and add_suffix() methods.
๐ŸŒ
Anyamemensah
anyamemensah.com โ€บ blog โ€บ renaming-python
Renaming Columns in Python โ€” Analytics Made Accessible
September 13, 2025 - To replace some or all of the column names, you can use a dictionary with old column names as keys and new column names as values and then pass this dictionary to the columns parameter. If you are unfamiliar with dictionaries and are a Python programmer/developer, I encourage you to learn more about them by reading through the ยท dictionaries documentation . They are powerful and useful tools that can help you solve a wide range of problems. Here's one way to implement a dictionary solution using Pandas: ### import library import pandas as pd ### Set up ## codebook # load codebook fruit_names_cb = pd.read_csv("fruit_codebook.csv") # create dictionary: keys are old names, values are new names fruit_names_dict = dict(zip(fruit_names_cb['old_name'], fruit_names_cb['new_name']))
๐ŸŒ
Medium
medium.com โ€บ @whyamit101 โ€บ pandas-set-column-names-a-comprehensive-guide-130c84f8761a
Pandas Set Column Names: A Comprehensive Guide | by why amit | Medium
April 12, 2025 - Yes, while pandas allows repeated column names, itโ€™s best practice to keep them unique to avoid confusion during data manipulation and analysis. How can I check the current column names in a DataFrame? You can check the column names of a DataFrame easily using df.columns, which returns an index object containing the list of column names. Setting and changing column names is an essential skill in data manipulation with pandas.
Find elsewhere
๐ŸŒ
Arab Psychology
scales.arabpsychology.com โ€บ home โ€บ how to easily rename columns in pandas dataframes
How To Easily Rename Columns In Pandas DataFrames
December 4, 2025 - Consider the scenario where we want to prefix all column names in our sample dataset (team, points, assists, rebounds) with an underscore to denote internal variables (e.g., _team, _points). This task is perfectly suited for direct assignment to df.columns, as illustrated in the following execution block. 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']
๐ŸŒ
pandas
pandas.pydata.org โ€บ Pandas_Cheat_Sheet.pdf pdf
with pandas Cheat Sheet http://pandas.pydata.org
Select single column with specific name. ... Select rows 10-20. ... Select all columns between x2 and x4 (inclusive). ... First index selects rows, second index columns. Cheatsheet for pandas (http://pandas.pydata.org/ originally written by Irv Lustig, Princeton Consultants, inspired by Rstudio Data Wrangling Cheatsheet
๐ŸŒ
KDnuggets
kdnuggets.com โ€บ 2022 โ€บ 11 โ€บ 4-ways-rename-pandas-columns.html
4 Ways to Rename Pandas Columns - KDnuggets
November 7, 2022 - The first method is quite simple. We will use the pandas `rename()` function to relabel the column names.
๐ŸŒ
scikit-learn
scikit-learn.org โ€บ stable โ€บ modules โ€บ generated โ€บ sklearn.model_selection.GridSearchCV.html
GridSearchCV โ€” scikit-learn 1.9.0 documentation
However computing the scores on the training set can be computationally expensive and is not strictly required to select the parameters that yield the best generalization performance. Added in version 0.19. Changed in version 0.21: Default value was changed from True to False ... A dict with keys as column headers and values as columns, that can be imported into a pandas DataFrame.
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ renaming column names in pandas
Renaming Column Names in Pandas
December 20, 2023 - The rename() function in Pandas allows us to rename column names by providing a dictionary-like object or a mapping function. We can specify the old column name as the key and the new column name as the value in the dictionary.
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ pandas-tips-renaming-columns
How to rename DataFrame columns in Pandas | Saturn Cloud Blog
September 10, 2023 - import pandas as pd data = pd.DataFrame({'a': [1, 2, 3], 'b': [10, 20, 30], 'c': [100, 200, 300]}) data.rename({'a': 'a1', 'b': 'b2'}, axis=1, inplace=True) data ยท You can also reassign all column names from a list by using set_axis():
๐ŸŒ
Altcademy
altcademy.com โ€บ blog โ€บ how-to-change-column-name-in-pandas
How to change column name in Pandas
January 10, 2024 - Pandas allows you to do this by using the names parameter in the read_csv function (or similar functions for other file types) and setting header=0 to replace the existing column names.
๐ŸŒ
Codegive
codegive.com โ€บ blog โ€บ pandas_change_first_column_name.php
Pandas Change First Column Name: The Ultimate Guide to Renaming Your Data (2024) โ€“ Unlock Cleaner Dataframes!
Programmatic Access: Clear names allow for intuitive access to data using df['column_name'] rather than relying on positional indexing. Integration with Other Tools: Many data analysis and visualization tools expect meaningful column headers. Pandas provides a few robust methods to change the first column name.
๐ŸŒ
Built In
builtin.com โ€บ data-science โ€บ rename-columns-pandas
How to Rename Columns in Pandas | Built In
Therefore, renaming columns is one of the essential steps that needs to be done at the beginning of your project. There are four common ways to rename a column in Pandas: Using dictionary with the pandas. DataFrame. rename() function.
๐ŸŒ
Kaggle
kaggle.com โ€บ code โ€บ dansbecker โ€บ your-first-machine-learning-model
Your First Machine Learning Model
April 21, 2023 - Explore and run AI code with Kaggle Notebooks | Using data from multiple data sources
๐ŸŒ
Kamatera
kamatera.com โ€บ home โ€บ knowledgebase โ€บ how to rename columns in pandas dataframes
How to Rename Columns in Pandas DataFrames | Kamatera
September 21, 2025 - The rename() method is one of the most flexible and commonly used approaches for renaming columns in pandas. This method allows you to selectively rename one or more columns without affecting others, making it ideal for targeted changes.
๐ŸŒ
Datablist
datablist.com โ€บ home โ€บ learn โ€บ csv manipulation โ€บ download sample csv files for free
Download Sample CSV Files for free - Datablist - Datablist
For each dataset, several CSV sizes are available, from 100 to 2 million records. The first line contains the CSV headers. An index column is set on each file.
๐ŸŒ
Matplotlib
matplotlib.org โ€บ stable โ€บ gallery โ€บ color โ€บ named_colors.html
List of named colors โ€” Matplotlib 3.10.9 documentation
import math import matplotlib.pyplot as plt import matplotlib.colors as mcolors from matplotlib.patches import Rectangle def plot_colortable(colors, *, ncols=4, sort_colors=True): cell_width = 212 cell_height = 22 swatch_width = 48 margin = 12 # Sort colors by hue, saturation, value and name. if sort_colors is True: names = sorted( colors, key=lambda c: tuple(mcolors.rgb_to_hsv(mcolors.to_rgb(c)))) else: names = list(colors) n = len(names) nrows = math.ceil(n / ncols) width = cell_width * ncols + 2 * margin height = cell_height * nrows + 2 * margin dpi = 72 fig, ax = plt.subplots(figsize=(widt
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ pandas โ€บ how-to-rename-columns-in-pandas-dataframe
Rename Columns in Pandas DataFrame - GeeksforGeeks
Assign a new list of names to df.columns. Ensure the list length matches the number of columns. Print df to verify the updated column names. The set_axis method can be used to rename all columns in a DataFrame.
Published ย  October 3, 2025