Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

pandas rename column by dictionary

# import pandas
import pandas as pd
 
# create data frame
df = pd.DataFrame({'First Name': ["Mukul", "Rohan", "Mayank",
                                  "Vedansh", "Krishna"],
                    
                   'Location': ["Saharanpur", "Rampur", "Agra",
                                "Saharanpur", "Noida"],
                    
                   'Pay': [56000.0, 55000.0, 61000.0, 45000.0, 62000.0]})
 
# print original data frame
display(df)
 
# create a dictionary
# key = old name
# value = new name
dict = {'First Name': 'Name',
        'Location': 'City',
        'Pay': 'Salary'}
 
# call rename () method
df.rename(columns=dict,
          inplace=True)
 
# print Data frame after rename columns
display(df)
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #pandas #rename #column #dictionary
ADD COMMENT
Topic
Name
2+7 =