I want to change the column labels of a Pandas DataFrame from
['$a', '$b', '$c', '$d', '$e']
to
['a', 'b', 'c', 'd', 'e']
dataframepandaspythonrenamereplace
I want to change the column labels of a Pandas DataFrame from
['$a', '$b', '$c', '$d', '$e']
to
['a', 'b', 'c', 'd', 'e']
Best Answer
Rename Specific Columns
Use the
df.rename()
function and refer the columns to be renamed. Not all the columns have to be renamed:Minimal Code Example
The following methods all work and produce the same output:
Remember to assign the result back, as the modification is not-inplace. Alternatively, specify
inplace=True
:You can specify
errors='raise'
to raise errors if an invalid column-to-rename is specified.Reassign Column Headers
Use
df.set_axis()
withaxis=1
.Headers can be assigned directly: