df = pd.DataFrame({'old_name_1': [1, 2, 3], 'old_name_2': [5, 6, 7]})
print(df)
# Output:
# old_name_1 old_name_2
# 0 1 5
# 1 2 6
# 2 3 7
To rename one or more columns, pass the old names and new names as a dictionary:
df.rename(columns={'old_name_1': 'new_name_1', 'old_name_2': 'new_name_2'}, inplace=True)
print(df)
# Output:
# new_name_1 new_name_2
# 0 1 5
# 1 2 6
# 2 3 7
Or a function:
df.rename(columns=lambda x: x.replace('old_', '_new'), inplace=True)
print(df)
# Output:
# new_name_1 new_name_2
# 0 1 5
# 1 2 6
# 2 3 7
You can also set df.columns
as the list of the new names:
df.columns = ['new_name_1','new_name_2']
print(df)
# Output:
# new_name_1 new_name_2
# 0 1 5
# 1 2 6
# 2 3 7
More details can be found here.