Renaming columns in pandas

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

    I needed to rename features for XGBoost, it didn't like any of these:

    import re
    regex = r"[!\"#$%&'()*+,\-.\/:;<=>?@[\\\]^_`{|}~ ]+"
    X_trn.columns = X_trn.columns.str.replace(regex, '_', regex=True)
    X_tst.columns = X_tst.columns.str.replace(regex, '_', regex=True)
    

提交回复
热议问题