简体   繁体   English

如何从熊猫数据框中删除带括号的特殊字符

[英]How to remove special character with brackets from pandas data frame

I have a columns values like this 'Voice_DropRate_4G(%)' so I need to be like this in the dataframe, just remove this '(%)' from all columns names, and I want also to replace the upper case letter with a lower case letter to be the final result like this in the data frame voice_droprate_4g我有一个像'Voice_DropRate_4G(%)'这样的列值,所以我需要在数据框中像这样,只需从所有列名中删除这个'(%)',我还想用小写字母替换大写字母在数据帧voice_droprate_4g大小写字母是这样的最终结果

as I tried to use this line to remove unneeded characters by using Replace function like the below code..因为我尝试使用此行通过使用如下代码的Replace功能来删除不需要的字符..

dft.columns = dft.columns.str.replace('(%)', '')

but it's doesn't remove the brackets it's shows me like this 'Voice_DropRate_4G()', So any one have any Idea how to solve this issue....但它并没有删除它向我展示的括号,就像这样的“Voice_DropRate_4G()”,所以任何人都知道如何解决这个问题......

Thank you...谢谢...

Because () are special regex chars you need escape it by \\ , then convert values to lowercase:因为()是特殊的正则表达式字符,您需要通过\\对其进行转义,然后将值转换为小写:

dft.columns = dft.columns.str.replace('\(%\)', '').str.lower()

Sample :样品

dft = pd.DataFrame(columns=['Voice_DropRate_4G(%)','Voice_DropRate_5G(%)'])
dft.columns = dft.columns.str.replace('\(%\)', '').str.lower()
print (dft)
Empty DataFrame
Columns: [voice_droprate_4g, voice_droprate_5g]
Index: []

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM