Cannot replace special characters in a Python pandas dataframe

我与影子孤独终老i 提交于 2021-02-07 09:55:47

问题


I'm working with Python 3.5 in Windows. I have a dataframe where a 'titles' str type column contains titles of headlines, some of which have special characters such as â,,˜.

I am trying to replace these with a space '' using pandas.replace. I have tried various iterations and nothing works. I am able to replace regular characters, but these special characters just don't seem to work.

The code runs without error, but the replacement simply does not occur, and instead the original title is returned. Below is what I have tried already. Any advice would be much appreciated.

df['clean_title'] = df['titles'].replace('€','',regex=True)
df['clean_titles'] = df['titles'].replace('€','')
df['clean_titles'] = df['titles'].str.replace('€','')

def clean_text(row):
   return re.sub('€','',str(row))
   return str(row).replace('€','')
df['clean_title'] = df['titles'].apply(clean_text)

回答1:


We can only assume that you refer to non-ASCI as 'special' characters.

To remove all non-ASCI characters in a pandas dataframe column, do the following:

df['clean_titles'] = df['titles'].str.replace(r'[^\x00-\x7f]', '')

Note that this is a scalable solution as it works for any non-ASCI char.



来源:https://stackoverflow.com/questions/50846719/cannot-replace-special-characters-in-a-python-pandas-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!