#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:
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:
If we apply this rename() function to our DataFrame, Pandas removes the "(Visitors)" part and the other column stay as it is:
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.