熊猫替换所有列名中的一个字符

我有数据帧与列名(来自。Csv 文件) ,其中包含 (),我想用 _替换它们。

我怎样才能在所有栏目中都做到这一点?

82476 次浏览

Use str.replace:

df.columns = df.columns.str.replace("[()]", "_")

Sample:

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

The square brackets are used to demarcate a range of characters you want extracted. for example:

r"[Nn]ational"

will extract both occurences where we have "National" and "national" i.e it extracts N or n.

Older pandas versions don't work with the accepted answer above. Something like this is needed:

df.columns = [c.replace("[()]", "_") for c in list(df.columns)]