Home > Mobile >  Replace or Remove special characters such as ' and " in pandas dataframe
Replace or Remove special characters such as ' and " in pandas dataframe

Time:07-23

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!

CodePudding user response:

Something like this?

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

Edit:

Use regex, thanks to @mozway

CodePudding user response:

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
  • Related