Renaming columns in pandas

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

    Assuming you can use regular expression. This solution removes the need of manual encoding using regex

    import pandas as pd
    import re
    
    srch=re.compile(r"\w+")
    
    data=pd.read_csv("CSV_FILE.csv")
    cols=data.columns
    new_cols=list(map(lambda v:v.group(),(list(map(srch.search,cols)))))
    data.columns=new_cols
    

提交回复
热议问题