Renaming columns in pandas

前端 未结 27 2596
野性不改
野性不改 2020-11-21 07:05

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

27条回答
  •  伪装坚强ぢ
    2020-11-21 07:24

    old_names = ['$a', '$b', '$c', '$d', '$e'] 
    new_names = ['a', 'b', 'c', 'd', 'e']
    df.rename(columns=dict(zip(old_names, new_names)), inplace=True)
    

    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.

提交回复
热议问题