Python: Pandas Rename Specific Column names in DataFrame Example

If you want to only rename specific column names in a Python pandas DataFrame, you can make use of the DataFrame.rename() function as follows.

Code Example:
import pandas as pd

df = pd.DataFrame({'Open Price': [14, 22, 45],
                   'High': [16, 22, 46],
                   'Low': [12, 20, 41],
                   'Close': [15, 21, 42]})

df = df.rename(columns={'High': 'Highest Price',
                        'Low': 'Lowest Price',
                        'Close': 'Closing Price'})


As you may see I have renamed only 3 columns specifically by providing a dictionary where the keys are the current column names and the values are the desired new column names, I have not changed the "Open Price" column as it looks good already!

Pandas Rename Specific Column Names in DataFrame Example


Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail:

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap