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.
The
rename
method can take a function, for example:You could use
str.slice
for that:Here's a nifty little function I like to use to cut down on typing:
Here is an example of how it works:
As documented in http://pandas.pydata.org/pandas-docs/stable/text.html:
If you have to deal with loads of columns named by the providing system out of your control, I came up with the following approach that is a combination of a general approach and specific replacments in one go.
I first create a dictionary from the dataframe column names using regex expressions in order to throw away certain appendixes of column names and then I add specific replacements to the dictionary to name core columns as expected later in the receiving database.
This is then applied to the dataframe in one go.