您好,登錄后才能下訂單哦!
這篇文章主要介紹“Pandas怎么讀取JSON數(shù)據(jù)”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“Pandas怎么讀取JSON數(shù)據(jù)”文章能幫助大家解決問(wèn)題。
使用的是pd.read_json函數(shù)
pandas.read_json( path_or_buf=None, # 文件路徑 orient=None, # 取值:split、records、index、columns、values typ='frame', # 要恢復(fù)的對(duì)象類(lèi)型(系列或框架),默認(rèn)'框架'. dtype=None, # boolean或dict,默認(rèn)為T(mén)rue convert_axes=None, convert_dates=True, keep_default_dates=True, numpy=False, precise_float=False, date_unit=None, encoding=None, # 編碼 lines=False, # 布爾值,默認(rèn)為False,每行讀取該文件作為json對(duì)象 chunksize=None, # 分塊讀取大小 compression='infer', nrows=None, storage_options=None)
模擬了一份數(shù)據(jù),vscode打開(kāi)內(nèi)容:
可以看到默認(rèn)情況下的讀取效果:
主要有下面幾個(gè)特點(diǎn):
第一層級(jí)字典的鍵當(dāng)做了DataFrame的字段
第二層級(jí)的鍵默認(rèn)當(dāng)做了行索引
下面重點(diǎn)解釋下參數(shù)orident
取值可以是:split、records、index、columns、values
json文件的key的名字只能為index,cloumns,data
;不多也不能少。
split' : dict like {index -> [index], columns -> [columns], data -> [values]}
In [3]:
data1 = '{"index":[1,2],"columns":["name","age"],"data":[["xiaoming",28], ["zhouhong",20]]}'
In [4]:
df1 = pd.read_json(data1, orient="split") df1
結(jié)果表明:
index:當(dāng)做行索引
columns:列名
data:具體的取值
如果我們改變其中一個(gè)key,比如data換成information就報(bào)錯(cuò)了:
當(dāng)orient="records"的時(shí)候,數(shù)據(jù)是以字段 + 取值的形式存放的。
‘records' : list like [{column -> value}, … , {column -> value}]
In [7]:
data2 = '[{"name":"Peter","sex":"male","age":20},{"name":"Tom","age":27},{"sex":"male"}]'
In [8]:
df2 = pd.read_json(data2, orient="records") df2
生成數(shù)據(jù)的特點(diǎn):
列表中元素是以字典的形式存放
列表中每個(gè)元素(字典)的key,如果沒(méi)有出現(xiàn)則取值為NaN
當(dāng)orient="index"的時(shí)候,數(shù)據(jù)是以行的形式來(lái)存儲(chǔ)。
dict like {index -> {column -> value}}
In [9]:
data3 = '{"id1":{"name":"Mike","age":20,"sex":"male","score":80},"id2":{"name":"Jack","sex":"female","score":90}}'
In [10]:
df3 = pd.read_json(data3, orient="index") df3
每個(gè)id存放一條數(shù)據(jù)
未出現(xiàn)的key取值為NaN
在這種情況下數(shù)據(jù)是以列的形式來(lái)存儲(chǔ)的。
dict like {column -> {index -> value}}
In [11]:
data4 = '{"sex":{"id1":"Peter","id2":"Tom","id3":"Jimmy"},"age":{"id1": 20,"id3":28}}'
In [12]:
df4 = pd.read_json(data4, orient="columns") df4
如果我們對(duì)上面的結(jié)果實(shí)施轉(zhuǎn)置(兩種方法):
我們會(huì)發(fā)現(xiàn)這個(gè)結(jié)果和orient="index"的讀取結(jié)果是相同的:
在這種情況下,數(shù)據(jù)是以數(shù)組的形式存在的:
‘values' : just the values array
In [16]:
data5 = '[["深圳",2000],["廣州",1900],["北京",2500]]'
In [17]:
df5 = pd.read_json(data5, orient="values") df5
對(duì)生成的列名進(jìn)行重新命名:
將DataFrame數(shù)據(jù)保存成json格式的文件
DataFrame.to_json(path_or_buf=None, # 路徑 orient=None, # 轉(zhuǎn)換類(lèi)型 date_format=None, # 日期轉(zhuǎn)換類(lèi)型 double_precision=10, # 小數(shù)保留精度 force_ascii=True, # 是否顯示中文 date_unit='ms', # 日期顯示最小單位 default_handler=None, lines=False, compression='infer', index=True, # 是否保留行索引 indent=None, # 空格數(shù) storage_options=None)
官網(wǎng)學(xué)習(xí)地址:
pandas.pydata.org/docs/refere…
1、默認(rèn)保存
df.to_json("df_to_json_1.json", force_ascii=True) # 不顯示中文
顯示結(jié)果為一行數(shù)據(jù),且存在unicode編碼,中文無(wú)法顯示:
{"sex":{"Jimmy":"male","Tom":"female","Jack":"male","Mike":"female"},"age":{"Jimmy":20,"Tom":18,"Jack":29,"Mike":26},"height":{"Jimmy":187,"Tom":167,"Jack":178,"Mike":162},"address":{"Jimmy":"\u6df1\u5733","Tom":"\u4e0a\u6d77","Jack":"\u5317\u4eac","Mike":"\u5e7f\u5dde"}}
2、顯示中文
df.to_json("df_to_json_2.json", force_ascii=False) # 顯示中文
中文能夠正常顯示:
{"sex":{"Jimmy":"male","Tom":"female","Jack":"male","Mike":"female"},"age":{"Jimmy":20,"Tom":18,"Jack":29,"Mike":26},"height":{"Jimmy":187,"Tom":167,"Jack":178,"Mike":162},"address":{"Jimmy":"深圳","Tom":"上海","Jack":"北京","Mike":"廣州"}}
3、不同的orient顯示 + 換行(indent參數(shù))
df.to_json("df_to_json_3.json", force_ascii=False, orient="index",indent=4) # index + 換行
顯示結(jié)果中鍵為name信息:
4、改變index
df.to_json("df_to_json_4.json", force_ascii=False, orient="columns",indent=4) # columns + 換行
關(guān)于“Pandas怎么讀取JSON數(shù)據(jù)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。