I have a DataFrame using pandas and column labels that I need to edit to replace the original column labels.
I'd like to change the column names in a DataFrame A
where the original column names are:
['$a', '$b', '$c', '$d', '$e']
to
['a', 'b', 'c', 'd', 'e'].
I have the edited column names stored it in a list, but I don't know how to replace the column names.
This way you can manually edit the
new_names
as you wish. Works great when you need to rename only a few columns to correct mispellings, accents, remove special characters etc.I know this question and answer has been chewed to death. But I referred to it for inspiration for one of the problem I was having . I was able to solve it using bits and pieces from different answers hence providing my response in case anyone needs it.
My method is generic wherein you can add additional delimiters by comma separating
delimiters=
variable and future-proof it.Working Code:
Output:
Try this. It works for me
Since you only want to remove the $ sign in all column names, you could just do:
OR
Just assign it to the
.columns
attribute:Pandas 0.21+ Answer
There have been some significant updates to column renaming in version 0.21.
rename
method has added theaxis
parameter which may be set tocolumns
or1
. This update makes this method match the rest of the pandas API. It still has theindex
andcolumns
parameters but you are no longer forced to use them.set_axis
method with theinplace
set toFalse
enables you to rename all the index or column labels with a list.Examples for Pandas 0.21+
Construct sample DataFrame:
Using
rename
withaxis='columns'
oraxis=1
or
Both result in the following:
It is still possible to use the old method signature:
The
rename
function also accepts functions that will be applied to each column name.or
Using
set_axis
with a list andinplace=False
You can supply a list to the
set_axis
method that is equal in length to the number of columns (or index). Currently,inplace
defaults toTrue
, butinplace
will be defaulted toFalse
in future releases.or
Why not use
df.columns = ['a', 'b', 'c', 'd', 'e']
?There is nothing wrong with assigning columns directly like this. It is a perfectly good solution.
The advantage of using
set_axis
is that it can be used as part of a method chain and that it returns a new copy of the DataFrame. Without it, you would have to store your intermediate steps of the chain to another variable before reassigning the columns.