简体   繁体   中英

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

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

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