0

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:
Json-format dataset 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?

4
  • Can you add sample of json as text? Commented Jan 25, 2017 at 9:12
  • Also check if json is valid - jsonlint.com Commented Jan 25, 2017 at 9:13
  • stackoverflow.com/questions/33590184/… Commented Jan 25, 2017 at 9:58
  • That's a good help for sure, But it doesn't fully answer the question, especially for the part which we need to extract the rows. Thanks for the help anyway. Commented Jan 25, 2017 at 12:31

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

I tried the code you have developed. Unfortunately it gives an error. File "C:\Users\masoud\Desktop\Dataset\data3\aa - Copy.py", line 16, in <module> key,value =item.split(":") ValueError: too many values to unpack (expected 2)
updated the answer, that the reason we expect OP to have minimum working inputs and expected outputs in question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.