Home > Net >  replace() method not working over single quote
replace() method not working over single quote

Time:12-24

I am trying to do:

df_flat = df_flat.replace("'", '"', regex=True)

To change single quotes to double quotes in the whole pandas df. I have the whole dataframe with single quotes because I applied df.json_normalize and this changed all double quotes from source to single quotes, so I want to recover the double quotes.

I tried this different options:

df_flat = df_flat.apply(lambda s:s.replace("'",'"', regex=True))
df_flat=df_flat.replace({'\'': '"'}, regex=True)

And any of them is working. Any idea of what's happening?

I have pandas==1.3.2.

And the content of the columns are like:

{'A':'1', 'NB':'29382', 'SS': '686'}

Edit:

I need it because I then save that pandas df in a parquet file and copy to AWS Redshift. When try to do json_extract_path it doesn't work as it's not a valid json due to the single quotes. I can do replace in Redshift for each field, but i prefer to store in the correct format.

CodePudding user response:

You may need to treat it as string:

df_flat = df_flat.astype(str).replace("'",'"', regex=True)
  • Related