Renaming columns in pandas

前端 未结 27 2542
野性不改
野性不改 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:23

    Another option is to rename using a regular expression:

    import pandas as pd
    import re
    
    df = pd.DataFrame({'$a':[1,2], '$b':[3,4], '$c':[5,6]})
    
    df = df.rename(columns=lambda x: re.sub('\$','',x))
    >>> df
       a  b  c
    0  1  3  5
    1  2  4  6
    

提交回复
热议问题