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>You can use the <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rename.html" rel="nofollow">DataFrame.rename()</a> method</p>

<pre><code>new_names = [(i,i+'_x') for i in df.iloc[:, 2:].columns.values]
df.rename(columns = dict(new_names), inplace=True)
</code></pre>

python pandas