Tuesday, 11 July 2017

python - Renaming columns in pandas



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.


Answer




Just assign it to the .columns attribute:



>>> df = pd.DataFrame({'$a':[1,2], '$b': [10,20]})
>>> df.columns = ['a', 'b']
>>> df
a b
0 1 10
1 2 20

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...