Pandas replace a character in all column names
<p>I have data frames with column names (coming from .csv files) containing <code>(</code> and <code>)</code> and I'd like to replace them with <code>_</code>.</p>

<p>How can I do that in place for all columns?</p>

<p>Use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.replace.html" rel="nofollow"><code>str.replace</code></a>:</p>

<pre><code>df.columns = df.columns.str.replace("[()]", "_")
</code></pre>

<p>Sample:</p>

<pre><code>df = pd.DataFrame({'(A)':[1,2,3],
                   '(B)':[4,5,6],
                   'C)':[7,8,9]})

print (df)
   (A)  (B)  C)
0    1    4   7
1    2    5   8
2    3    6   9

df.columns = df.columns.str.replace(r"[()]", "_")
print (df)
   _A_  _B_  C_
0    1    4   7
1    2    5   8
2    3    6   9
</code></pre>

python pandas