我想要读取一个从dict()生成的JSON文件,这样我就可以制作一个饼图。到目前为止我的代码如下:
import json
import pandas as pd
openJson = open("path")
jsonFile = json.load(openJson)
df = pd.DataFrame.from_dict(jsonFile)我遇到的问题是,我甚至不能尝试绘制图形,因为我不能将JSON转换为数据框。我得到的错误是ValueError: If using all scalar values, you must pass an index。我也尝试过编写类似帖子中的df = pd.DataFrame.from_dict(jsonFile, index=[0]),但似乎index是一个意想不到的参数。
如何读取JSON才能绘制出来呢?
LE:添加了名为category.json的JSON文件
{"Restaurants": 678.7800000000001, "Utilities": 807.26, "Services": 35.67, "Transport": 1295.65, "Shopping": 1454.15, "Groceries": 1162.89}发布于 2020-09-28 20:13:54
试试下面的
import pandas as pd
import json
with open('data.json') as f:
df = pd.DataFrame(json.load(f),index=[0])
print(df)输出
Restaurants Utilities ... Shopping Groceries
0 678.78 807.26 ... 1454.15 1162.89
[1 rows x 6 columns]发布于 2020-09-28 20:00:13
像这样读取JSON文件
df = pd.read_json (r'C:\Users\XXX\Desktop\data.json')如果您的文件已经是JSON格式,这将会起作用。
https://stackoverflow.com/questions/64101667
复制相似问题