Rename columns within Pandas Dataframe in Python
Problem description
How to rename the columns within a pandas DataFrame in Python?
Problem solution
To rename columns you can use the DataFrame.rename() method. Don't forget to specify inplace=True to modify the existing DataFrame and not creating a new one. Here is an example:
DataFrame before:
a b
0 x x
1 x x
2 x x
Piece of code:
df.rename(columns={'a': 'X', 'b': 'Y'}, inplace=True)
DataFrame after:
x y
0 x x
1 x x
2 x x