How to add a suffix (or prefix) to each column name?

前端 未结 6 2112
-上瘾入骨i
-上瘾入骨i 2020-11-28 23:12

I want to add _x suffix to each column name like so:

featuresA = myPandasDataFrame.columns.values + \'_x\'

How do I do this? A

6条回答
  •  旧时难觅i
    2020-11-28 23:49

    Elegant In-place Concatenation

    If you're trying to modify df in-place, then the cheapest (and simplest) option is in-place addition directly on df.columns (i.e., using Index.__iadd__).

    df = pd.DataFrame({"A": [9, 4, 2, 1], "B": [12, 7, 5, 4]})
    df
    
       A   B
    0  9  12
    1  4   7
    2  2   5
    3  1   4
    

    df.columns += '_some_suffix'
    df
    
       A_some_suffix  B_some_suffix
    0              9             12
    1              4              7
    2              2              5
    3              1              4
    

    To add a prefix, you would similarly use

    df.columns = 'some_prefix_' + df.columns
    df
    
       some_prefix_A  some_prefix_B
    0              9             12
    1              4              7
    2              2              5
    3              1              4
    

    Another cheap option is using a list comprehension with f-string formatting (available on python3.6+).

    df.columns = [f'{c}_some_suffix' for c in df]
    df
    
       A_some_suffix  B_some_suffix
    0              9             12
    1              4              7
    2              2              5
    3              1              4
    

    And for prefix, similarly,

    df.columns = [f'some_prefix{c}' for c in df]
    

    Method Chaining

    It is also possible to do add *fixes while method chaining. To add a suffix, use DataFrame.add_suffix

    df.add_suffix('_some_suffix')
    
       A_some_suffix  B_some_suffix
    0              9             12
    1              4              7
    2              2              5
    3              1              4
    

    This returns a copy of the data. IOW, df is not modified.

    Adding prefixes is also done with DataFrame.add_prefix.

    df.add_prefix('some_prefix_')
    
       some_prefix_A  some_prefix_B
    0              9             12
    1              4              7
    2              2              5
    3              1              4
    

    Which also does not modify df.


    Critique of add_*fix

    These are good methods if you're trying to perform method chaining:

    df.some_method1().some_method2().add_*fix(...)
    

    However, add_prefix (and add_suffix) creates a copy of the entire dataframe, just to modify the headers. If you believe this is wasteful, but still want to chain, you can call pipe:

    def add_suffix(df):
        df.columns += '_some_suffix'
        return df
    
    df.some_method1().some_method2().pipe(add_suffix)
    

提交回复
热议问题