简体   繁体   中英

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

as I tried to use this line to remove unneeded characters by using Replace function like the below code..

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....

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: []

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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