简体   繁体   English

替换或删除熊猫数据框中的特殊字符,例如“和”

[英]Replace or Remove special characters such as ' and " in pandas dataframe

In the data frame that I am working on, there are several columns that contain special characters such as " and ' . They are either at the end or in the beginning of the column name.在我正在处理的数据框中,有几列包含特殊字符,例如 " 和 ' 。它们位于列名的末尾或开头。

How can I get rid of them?我怎样才能摆脱它们? Is there any chance to read files with these characters?有没有机会读取这些字符的文件?

I have tried several options, however, it did not work.我已经尝试了几个选项,但是,它没有工作。

Examples of the columns are following:列的示例如下:

est_soilty_Gh''

upd_siffer_Kh'g

est_soilty_M'''

Thanks in advance for your assistance!提前感谢你的帮助!

Something like this?像这样的东西?

df.column_name = df.column_name.str.replace(r'["\']', '')

Edit:编辑:

Use regex, thanks to @ mozway使用正则表达式,感谢@mozway

Another option:另外的选择:

df = pd.DataFrame({"est_soilty_Gh''": [1,2,4],
                    "upd_siffer_Kh'g": [0,0.2,0.5],
                    "est_soilty_M'''": [2,3,4]})



    est_soilty_Gh''  upd_siffer_Kh'g  est_soilty_M'''
0                1              0.0                2
1                2              0.2                3
2                4              0.5                4
df.columns = df.columns.str.replace(r"'", '')


print(df)

est_soilty_Gh  upd_siffer_Khg  est_soilty_M
0              1             0.0             2
1              2             0.2             3
2              4             0.5             4

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

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