【发布时间】:2021-03-23 10:12:45
【问题描述】:
我有一个包含 dict 值的 CSV 文件。
Test.CSV
id,name,contact,Location
1,Julie,"[{""name"":""Jenny Brown"",""relation"":""mother"",""number"":2113131313},{""name"":""Jorge"",""relation"":""brother"",""number"":121313131}]",US
2,Jim,"[{""name"":""Sana"",""relation"":""sister"",""number"":83279131}]",UK
我想规范化这个 CSV。预期输出:
id , name, contact_name,contact_realation,contact_number,location
1,Julie,Jenny Brown,mother,2113131313,US
1,Julie,Jorge,brother,121313131,US
2,Jim,Sana,sister,83279131,UK
我已经使用 CSV 阅读器加载了数据,但我无法标准化联系人值。我该怎么做?
csvfile = csv.reader(open(filename, encoding="utf8"))
到目前为止,我尝试过这个:
df=pd.read_csv(filename, converters={'contact':json.loads}, header=0)
contact_df = pd.io.json.json_normalize(df['contact'])
但出现以下错误:
AttributeError: 'list' object has no attribute 'values'
【问题讨论】:
-
解析每行的 json 块,然后从感兴趣的属性组装你的新行。
-
嗨,我已经尝试过了,但出现错误:df=pd.read_csv(filename, converters={'contact':json.loads}, header=0) contact_df = pd.io.json。 json_normalize(df['contact'])
-
在问题中添加您尝试过的内容以及失败的原因
标签: python python-3.x pandas python-2.7