简体   繁体   English

将单引号替换为双引号 python pandas dataframe

[英]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.我想将单引号(')替换为双引号(“),使其成为python数据帧中正确的json列值。

eg csv file looks like...例如 csv 文件看起来像...

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.我正在从 csv 到 pandas 数据帧读取这些值。 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:如果问题是将单引号转换为双引号,在将其读入数据帧后却没有限制地执行此操作 - 您可以将其读入数据帧之前更改.csv文件:

$ 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 .使用str.replace

If you want to update a column on a DataFrame, such as this如果要更新 DataFrame 上的列,例如

数据帧示例

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这是最终结果

运行上面的代码后的输出

You can convert values to dictionaries like:您可以将值转换为字典,例如:

import ast

df['<column_name>'] = df['<column_name>'].apply(ast.literal_eval)

But if input data are json file (string), better is use json_normalize .但如果输入数据是json文件(字符串),最好使用json_normalize

Looks like you need.看起来你需要。

import pandas as pd
import json
import ast


df = pd.DataFrame({"Unit Id": ["A108", "P258"], "Batch Id": ["qa120", "re015"], "Items prod": ["{'A': 123, 'B': 342, 'C': 454}", "{'A': 124, 'B': 234, 'C': 343}"]})
df["NEW"] = df["Items prod"].apply(ast.literal_eval).apply(json.dumps)
print(df)

Output:输出:

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

                              NEW  
0  {"A": 123, "C": 454, "B": 342}  
1  {"A": 124, "C": 343, "B": 234}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM