rename columns pandas
df.rename(columns={'oldName1': 'newName1',
'oldName2': 'newName2'},
inplace=True, errors='raise')
# Make sure you set inplace to True if you want the change
# to be applied to the dataframe
rename columns pandas
df.rename(columns={'oldName1': 'newName1',
'oldName2': 'newName2'},
inplace=True, errors='raise')
# Make sure you set inplace to True if you want the change
# to be applied to the dataframe
How to rename columns in Pandas DataFrame
# import pandas library
import pandas as pd
# create pandas DataFrame
df = pd.DataFrame({'team': ['India', 'South Africa', 'New Zealand', 'England'],
'points': ['10', '8', '3', '5'],
'runrate': ['0.5', '1.4', '2', '-0.6'],
'wins': ['5', '4', '2', '2']})
# print the column names of DataFrame
print(list(df))
# rename the column names of DataFrame
df.rename(columns={'points': 'total_points',
'runrate': 'run_rate'}, inplace=True)
# print the new column names of DataFrame
print(list(df))
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us