Python: Pandas Rename Columns with List Example


import pandas as pd

df = pd.DataFrame({
    'Col1': ['2023-01','2023-02', '2023-03'],
    'Col2': ['Delhi', 'New York','Tokyo'],
    'Col3': [31,19,22]
})

new_column_names = ['Date', 'City', 'Temp']

df.rename(columns=dict(zip(df.columns, new_column_names)), inplace=True)

print(df)
Output:
      Date      City  Temp
0  2023-01     Delhi    31
1  2023-02  New York    19
2  2023-03     Tokyo    22
Python - Pandas Rename Columns with List Example

Reference: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rename.html

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: rakesh@code2care.org

Copyright ยฉ Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap