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.
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)
Use str.replace
.
If you want to update a column on a DataFrame, such as this
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With