0

I want to read a JSON file generated from a dict() such that I can then make a pie chart. My code so far:

import json
import pandas as pd

openJson = open("path")

jsonFile = json.load(openJson)

df = pd.DataFrame.from_dict(jsonFile)

The problem I have is that I can't even try plotting the graph because I cant convert the JSON to a data frame. The error I get is ValueError: If using all scalar values, you must pass an index. I also tried writing df = pd.DataFrame.from_dict(jsonFile, index=[0]) as found in a similar post, but it seems that index is an unexpected argument.

How can I read that JSON so it can be plotted?

LE: Added JSON file called category.json

{"Restaurants": 678.7800000000001, "Utilities": 807.26, "Services": 35.67, "Transport": 1295.65, "Shopping": 1454.15, "Groceries": 1162.89}
Andrei
  • 59
  • 5

2 Answers2

1

Read JSON file like this

df = pd.read_json (r'C:\Users\XXX\Desktop\data.json')

This will work if your file is already in JSON format.

vuun0
  • 153
  • 1
  • 9
0

Try the below

import pandas as pd
import json
with open('data.json') as f:
    df = pd.DataFrame(json.load(f),index=[0])
print(df)

output

   Restaurants  Utilities  ...  Shopping  Groceries
0       678.78     807.26  ...   1454.15    1162.89

[1 rows x 6 columns]
balderman
  • 22,927
  • 7
  • 34
  • 52
  • sorry for reaching out again, but I tried e simple pie chart plot `df.plot(subplots=True, kind='pie')` but nothing shows. Weirdly, PyCharm displays no error. – Andrei Sep 28 '20 at 13:05
  • see https://stackoverflow.com/questions/34347145/pandas-plot-doesnt-show – balderman Sep 28 '20 at 13:12