Reputation: 1122
I am working on a data mining project. I need to read data from a json-format dataset which belongs to amazon.
The format of dataset is like this:
First I want to extract these rows:
[productName], [rating]
And after that I want to write the rows to a csv file with two columns named as productName and Rating. Is there any way to implement this by using pandas library?
Upvotes: 0
Views: 1275
Reputation: 9711
With a subset of data, i have converted it into DF.Note that the data you have is not a json formatted data.
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
Upvotes: 1