首页 > 解决方案 > 使用正则表达式从许多列名中删除句点

问题描述

我想删除一个'。使用正则表达式从列名中提取代码,并希望将代码应用于许多以“。”结尾的列名,以便可以将每对同名列合并为一个。

例如,列名“Fund”和“Fund”。不同,有不同的价值,但应该只是“基金”。

为此使用的最佳正则表达式是什么?

标签: pythonregexpandastransform

解决方案


尝试这个:

df = pd.DataFrame([1], columns=['Fund.'])
df.columns = df.columns.str.replace('.','')

输出:

print(df.columns)

Index(['Fund'], dtype='object')

推荐阅读