0

I am trying to convert this dict to a DataFrame

My Json dict = {'comments': "['a','b','c']"}

My code:

contents = open('comments.json', "r").read()
json_obj = json.loads(contents)
pd.DataFrame.from_dict(json_obj)

It gave me this error : ValueError: If using all scalar values, you must pass an index

so I tried this:

pd.DataFrame.from_dict(json_obj)

Which gave me this df:

enter image description here

I need each item of this list to be in a row.

What I tried:

d = pd.DataFrame.from_dict([json_obj])
pd.DataFrame.from_records(d.comments)

But it split each letter to a column like this:

[![enter image description here][1]][1]

I also tried all the solutions related to this question but none of them work

Please note that my data is originally a list of arabic items and I dumped it to a file as a dictionary:

json.dump({'comments': tmp1},fp,indent = 4, ensure_ascii=False)

I don't understand what is going on and will appreciate any help!

enter image description here

J.Doe
  • 315
  • 2
  • 10
  • Please provide a [mcve], including sample input/output. – AMC Mar 04 '20 at 02:22
  • 1
    If your json dict is `{'comments': "['a','b','c']"}`, the value is a string representation of a list but not a list. See [this](https://stackoverflow.com/questions/1894269/convert-string-representation-of-list-to-list) post on how to convert the string back to a proper list. – Henry Yik Mar 04 '20 at 02:49
  • Your comment directed me to this solution which solved my problem! Please write your comments as an answer so I can mark it. Thank you very https://stackoverflow.com/questions/44672561/convert-string-representation-of-list-into-list – J.Doe Mar 04 '20 at 08:12

0 Answers0