[英]How to read data from json file and convert it to csv using pandas?
I am working on a data mining project. 我正在做一个数据挖掘项目。 I need to read data from a json-format dataset which belongs to amazon.
我需要从属于亚马逊的json格式数据集中读取数据。
The format of dataset is like this: 数据集的格式如下:
First I want to extract these rows:
首先,我要提取这些行:
[productName], [rating] [productName],[rating]
And after that I want to write the rows to a csv file with two columns named as productName and Rating. 之后,我想将行写入具有两个列分别为productName和Rating的csv文件中。 Is there any way to implement this by using pandas library?
有什么方法可以使用pandas库来实现吗?
With a subset of data, i have converted it into DF.Note that the data you have is not a json formatted data. 有了一部分数据,我已经将其转换为DF。请注意,您拥有的数据不是json格式的数据。
import pandas as pd
import json
from collections import defaultdict
import re
f=open('inv.json')
text= f.readlines()
RowID=[]
result={}
for item in text:
if item.startswith("###"):
RowID=re.findall('\d+', item)
result[RowID[0]]={}
elif ":" in item:
key,value =item.split(":",1)
result[RowID[0]][key.strip()]=value.strip()
df= pd.DataFrame(result)
print df.transpose()
sample input 样本输入
#####1
[ID]:0
[ProductId]:0
[rating]:2.0
#####2
[ID]:1
[ProductId]:2
[rating]:3.0
[fullText]:It is a good
[weburl]:http://example.org:xx
output 输出
[ID] [ProductId] [fullText] [rating] [weburl]
1 0 0 NaN 2.0 NaN
2 1 2 It is a good 3.0 http://example.org:xx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.