Renaming columns in pandas

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

    Another way we could replace the original column labels is by stripping the unwanted characters (here '$') from the original column labels.

    This could have been done by running a for loop over df.columns and appending the stripped columns to df.columns.

    Instead , we can do this neatly in a single statement by using list comprehension like below:

    df.columns = [col.strip('$') for col in df.columns]
    

    (strip method in Python strips the given character from beginning and end of the string.)

提交回复
热议问题