Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace single quote to double quote python pandas dataframe

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.

like image 330
Ruchita P Avatar asked Jan 28 '23 05:01

Ruchita P


2 Answers

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)

like image 137
darthbhyrava Avatar answered Jan 29 '23 18:01

darthbhyrava


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

like image 26
Gonçalo Peres Avatar answered Jan 29 '23 20:01

Gonçalo Peres