Skip to content

#254: Rename Columns in Pandas

While it is possible to fix the name of a column later in the plot, it is an additional step that takes time. It would be better if the data source already would have a correct name, but that is not always the case and often we cannot change that either. The remaining middle ground is to rename the column in the DataFrame - let us figure out how to do that.

The problem

The DataFrame I have had "(Visitors)" as part of 2 of its columns that I would like to go away:

1
2
3
4
5
>>> print(df)
         Date  JetPack (Visitors)  Statify  WP Statistics (Visitors)
0  2024-10-29                 347      499                       493
1  2024-10-30                 362      480                       476
2  2024-10-31                 251      346                       343

The rename() function

Pandas offers us a rename() function that accepts a dictionary with the old name as the key and the new name as the value. If we do not specify the axis, it will rename the rows in the index column. Since we want to rename the columns, we must use axis=1:

1
2
3
df = df.rename({'JetPack (Visitors)': 'JetPack', 
                'WP Statistics (Visitors)': 'WP Statistics'}, 
                axis=1)

If we apply this rename() function to our DataFrame, Pandas removes the "(Visitors)" part and the other column stay as it is:

1
2
3
4
5
>>> print(df)
         Date  JetPack  Statify  WP Statistics
0  2024-10-29      347      499            493
1  2024-10-30      362      480            476
2  2024-10-31      251      346            343

Conclusion

The rename() function is a great help to clean-up our DataFrames. It is not the simplest method call then the axis parameter is important yet easy to overlook.

Next week we jump to a totally different topic and fix the metadata of audio files.