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
df = 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)
df_new = df.rename(columns={'A':'a'}, index={'ONE':'one'})print(df_new)# a B C# one 11 12 13# TWO 21 22 23# THREE 31 32 33print(df)# A B C# ONE 11 12 13# TWO 21 22 23# THREE 31 32 33
# Simple use case for pd.rename()'''
old parameter = 'Data.Population'
new parameter = 'Population'
df.rename(columns={'old parameter': 'new parameter'}, inplace = True)
inplace = True : means to change object in real time
'''# view below for visual aids
df.rename(columns={'Data.Population':'Population'}, inplace =True)# old columns|'Data.Population'||_________________||0||_________________|# new output:# new rename column|'Population'||_____________||0||_____________|
# import pandas libraryimport 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 DataFrameprint(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 DataFrameprint(list(df))
df2.columns = stocks['Ticker'][:3][:3]is just use first 3[5::] skip first 5
price price price
2021-01-11131.9015.00179.072021-01-12128.0915.74182.65
to
Ticker A AAL AAP
2021-01-11131.9015.00179.072021-01-12128.0915.74182.65