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.
Note that these approach do not work for a MultiIndex. For a MultiIndex, you need to do something like the following:
The rename dataframe columns and replace format
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rename.html
Another option is to rename using a regular expression:
It will replace the existing names with the names you provide, in the order you provide.
df = df.rename(columns=lambda n: n.replace('$', ''))
is a functional way of solving this