replace single quote to double quote python pandas dataframe

12,852

Solution 1

If the problem is converting the single quote to double quotes without the restraint of doing it after you read it into a dataframe - you could change the .csv file before you read it into a dataframe:

$ sed -i "s/'/\"/g" file_name.csv

If you have to replace them after you read them into a dataframe, try the solution mentioned in this post:

df.replace({'\'': '"'}, regex=True)

Solution 2

Use str.replace.

If you want to update a column on a DataFrame, such as this

Example of DataFram

And let's say that you want to remove the double quotes from the first column.

Just do the following

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

Here is the final result

Output after running the code above

Share:
12,852
Ruchita P
Author by

Ruchita P

I have 8+ years of experience into Database development in Oracle PLSQL, MS SQL Server, MYSQL, PostgreSQL

Updated on June 15, 2022

Comments

  • Ruchita P
    Ruchita P almost 2 years

    I want to replace single quote(') to double quote(") to make it proper json column value in python dataframe.

    e.g. csv file looks like...

    Unit Id Batch Id                               Items prod
    A108    qa120  {'A': 123, 'B': 342, 'C': 454}   
    P258    re015  {'A': 124, 'B': 234, 'C': 343} 
    

    I'm reading these values from csv to pandas dataframe. I tried several ways, but no luck.

    df.replace("'",'"',inplace=True)
    df.['<column_name>'].str.replace(r"[\',]",'"')
    df = df['<column_name>'].str.replace(r"[\',]",'"')
    

    Thanks for your help in advance.

  • Ruchita P
    Ruchita P over 5 years
    Thank you so much, this works as expected. only minor change would be, need to assign this back to df. e.g. df = df.replace({'\'': '"'}, regex=True)
  • Abhishek Jain
    Abhishek Jain over 2 years
    if dataframe is going to be same then we can add another option inplace as True to avoid one line of code.... df = df.replace({'\'': '"'}, regex=True, inplace=True)