1

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!

YIF99
  • 51
  • 1
  • 8

2 Answers2

2

Something like this?

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

Edit:

Use regex, thanks to @mozway

Michael S.
  • 3,050
  • 4
  • 19
  • 34
2

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
ragas
  • 848
  • 2
  • 7
  • Thanks a lot! it worked as well. When there is ' , it works fine. As to " , I think I would need to use escape character – YIF99 Jul 24 '22 at 21:48