我想讀取從這樣生成的 JSON 文件,dict()然后我可以制作餅圖。到目前為止我的代碼:import jsonimport pandas as pd
openJson = open("path")
jsonFile = json.load(openJson)
df = pd.DataFrame.from_dict(jsonFile)我遇到的問題是我什至無法嘗試?yán)L制圖表,因?yàn)槲覠o法將 JSON 轉(zhuǎn)換為數(shù)據(jù)框。我得到的錯(cuò)誤是ValueError: If using all scalar values, you must pass an index. 我也嘗試df = pd.DataFrame.from_dict(jsonFile, index=[0])按照類似帖子中的內(nèi)容進(jìn)行寫作,但這似乎index是一個(gè)意想不到的論點(diǎn)。我如何讀取該 JSON 以便將其繪制出來?LE:添加了名為的 JSON 文件category.json{"Restaurants": 678.7800000000001, "Utilities": 807.26, "Services": 35.67, "Transport": 1295.65, "Shopping": 1454.15, "Groceries": 1162.89}
2 回答

MMTTMM
TA貢獻(xiàn)1869條經(jīng)驗(yàn) 獲得超4個(gè)贊
像這樣讀取 JSON 文件
df = pd.read_json (r'C:\Users\XXX\Desktop\data.json')
如果您的文件已經(jīng)是 JSON 格式,這將起作用。

回首憶惘然
TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超11個(gè)贊
嘗試下面的方法
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]
添加回答
舉報(bào)
0/150
提交
取消