Add prefix to specific columns of Dataframe
<p>I've a DataFrame like that :</p>

<pre><code>col1   col2   col3   col4   col5   col6   col7   col8
0      5345   rrf    rrf    rrf    rrf    rrf    rrf
1      2527   erfr   erfr   erfr   erfr   erfr   erfr
2      2727   f      f      f      f      f      f
</code></pre>

<p>I would like to rename all columns but not <strong>col1</strong> and <strong>col2</strong>.</p>

<p>So I tried to make a loop</p>

<pre><code>print(df.columns)
    for col in df.columns:
        if col != 'col1' and col != 'col2':
            col.rename = str(col) + '_x'
</code></pre>

<p>But it's not very efficient...it doesn't work !</p>

<p>Simpliest solution if <code>col1</code> and <code>col2</code> are first and second column names:</p>

<pre><code>df.columns = df.columns[:2].union(df.columns[2:]  + '_x')
print (df)
   col1  col2 col3_x col4_x col5_x col6_x col7_x col8_x
0     0  5345    rrf    rrf    rrf    rrf    rrf    rrf
1     1  2527   erfr   erfr   erfr   erfr   erfr   erfr
2     2  2727      f      f      f      f      f      f
</code></pre>

<p>Another solution with <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.isin.html" rel="nofollow"><code>isin</code></a> or list comprehension:</p>

<pre><code>cols = df.columns[~df.columns.isin(['col1','col2'])]
print (cols)
['col3', 'col4', 'col5', 'col6', 'col7', 'col8']

df.rename(columns = dict(zip(cols, cols + '_x')), inplace=True)

print (df)

   col1  col2 col3_x col4_x col5_x col6_x col7_x col8_x
0     0  5345    rrf    rrf    rrf    rrf    rrf    rrf
1     1  2527   erfr   erfr   erfr   erfr   erfr   erfr
2     2  2727      f      f      f      f      f      f
</code></pre>

<hr>

<pre><code>cols = [col for col in df.columns if col not in ['col1', 'col2']]
print (cols)
['col3', 'col4', 'col5', 'col6', 'col7', 'col8']

df.rename(columns = dict(zip(cols, cols + '_x')), inplace=True)

print (df)

   col1  col2 col3_x col4_x col5_x col6_x col7_x col8_x
0     0  5345    rrf    rrf    rrf    rrf    rrf    rrf
1     1  2527   erfr   erfr   erfr   erfr   erfr   erfr
2     2  2727      f      f      f      f      f      f
</code></pre>

<p>The fastest is list comprehension:</p>

<pre><code>df.columns = [col+'_x' if col != 'col1' and col != 'col2' else col for col in df.columns]
</code></pre>

<p><strong>Timings</strong>:</p>

<pre><code>In [350]: %timeit (akot(df))
1000 loops, best of 3: 387 Âµs per loop

In [351]: %timeit (jez(df1))
The slowest run took 4.12 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 207 Âµs per loop

In [363]: %timeit (jez3(df2))
The slowest run took 6.41 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 75.7 Âµs per loop
</code></pre>

<hr>

<pre><code>df1 = df.copy()
df2 = df.copy()

def jez(df):
    df.columns = df.columns[:2].union(df.columns[2:]  + '_x')
    return df

def akot(df):
    new_names = [(i,i+'_x') for i in df.iloc[:, 2:].columns.values]
    df.rename(columns = dict(new_names), inplace=True)
    return df


def jez3(df):
   df.columns = [col + '_x' if col != 'col1' and col != 'col2' else col for col in df.columns]
   return df


print (akot(df))
print (jez(df1))
print (jez2(df1))
</code></pre>

python pandas