Pandas實(shí)現(xiàn)解析JSON數(shù)據(jù)與導(dǎo)出的示例詳解
使用pandas解析JSON Dataset要方便得多。Pandas允許您將列表的列表轉(zhuǎn)換為Dataframe并單獨(dú)指定列名。JSON解析器將JSON文本轉(zhuǎn)換為另一種表示必須接受符合JSON語法的所有文本。它可以接受非JSON形式或擴(kuò)展。實(shí)現(xiàn)可以設(shè)置以下內(nèi)容:
- 它接受的文本大小的限制,
- 對嵌套的最大深度的限制,
- 對數(shù)字范圍和精度的限制,
- 設(shè)置字符串的長度和字符內(nèi)容的限制。
使用大型JSON數(shù)據(jù)集可能會惡化,特別是當(dāng)它們太大而無法容納在內(nèi)存中時。在這種情況下,命令行工具和Python的組合可以成為探索和分析數(shù)據(jù)的有效方法。
導(dǎo)入JSON文件
JSON的操作是使用Python數(shù)據(jù)分析庫pandas完成的。
import pandas as pd
現(xiàn)在,您可以使用命令read_json讀取JSON并將其保存為pandas數(shù)據(jù)結(jié)構(gòu)。
pandas.read_json (path_or_buf=None, orient = None, typ=’frame’, dtype=True, convert_axes=True, convert_dates=True, keep_default_dates=True, numpy=False, precise_float=False, date_unit=None, encoding=None, lines=False, chunksize=None, compression=’infer’)
import pandas as pd # Creating Dataframe df = pd.DataFrame([['a', 'b'], ['c', 'd']], index =['row 1', 'row 2'], columns =['col 1', 'col 2']) # Indication of expected JSON string format print(df.to_json(orient ='split')) print(df.to_json(orient ='index'))
輸出:
{"columns":["col 1", "col 2"],
"index":["row 1", "row 2"],
"data":[["a", "b"], ["c", "d"]]}
{"row 1":{"col 1":"a", "col 2":"b"},
"row 2":{"col 1":"c", "col 2":"d"}}
轉(zhuǎn)換object對象到j(luò)son數(shù)據(jù)使用dataframe.to_json
DataFrame.to_json(path_or_buf=None, orient=None, date_format=None, double_precision=10, force_ascii=True, date_unit=’ms’, default_handler=None, lines=False, compression=’infer’, index=True)
直接從Dataset讀取JSON文件:
import pandas as pd data = pd.read_json('http://api.population.io/1.0/population/India/today-and-tomorrow/?format = json') print(data)
輸出:
total_population
0 {'date': '2019-03-18', 'population': 1369169250}
1 {'date': '2019-03-19', 'population': 1369211502}
使用Pandas進(jìn)行嵌套JSON解析
嵌套的JSON文件可能非常耗時,并且很難將其展平并加載到Pandas中。
我們使用嵌套的“'raw_nyc_phil.json。"'從一個嵌套數(shù)組創(chuàng)建一個扁平化的pandas數(shù)據(jù)框,然后解包一個深度嵌套數(shù)組。
import json import pandas as pd from pandas.io.json import json_normalize with open('https://github.com/a9k00r/python-test/blob/master/raw_nyc_phil.json') as f: d = json.load(f) # lets put the data into a pandas df # clicking on raw_nyc_phil.json under "Input Files" # tells us parent node is 'programs' nycphil = json_normalize(d['programs']) nycphil.head(3)
works_data = json_normalize(data = d['programs'], record_path ='works', meta =['id', 'orchestra', 'programID', 'season']) works_data.head(3)
soloist_data = json_normalize(data = d['programs'], record_path =['works', 'soloists'], meta =['id']) soloist_data.head(3)
將Pandas DataFrame導(dǎo)出到JSON文件
讓我們看看如何將Pandas DataFrame導(dǎo)出為JSON文件。要執(zhí)行此任務(wù),我們將使用DataFrame.to_json()和pandas.read_json()函數(shù)。
示例1:
# importing the module import pandas as pd # creating a DataFrame df = pd.DataFrame([['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']], index =['row 1', 'row 2', 'row3'], columns =['col 1', 'col 2', 'col3']) # storing the data in JSON format df.to_json('file.json', orient = 'split', compression = 'infer', index = 'true') # reading the JSON file df = pd.read_json('file.json', orient ='split', compression = 'infer') # displaying the DataFrame print(df)
我們可以看到DataFrame已經(jīng)導(dǎo)出為JSON文件。
示例2:
# importing the module import pandas as pd # creating a DataFrame df = pd.DataFrame(data = [['15135', 'Alex', '25 / 4/2014'], ['23515', 'Bob', '26 / 8/2018'], ['31313', 'Martha', '18 / 1/2019'], ['55665', 'Alen', '5 / 5/2020'], ['63513', 'Maria', '9 / 12 / 2020']], columns =['ID', 'NAME', 'DATE OF JOINING']) # storing data in JSON format df.to_json('file1.json', orient = 'split', compression = 'infer') # reading the JSON file df = pd.read_json('file1.json', orient ='split', compression = 'infer') print(df)
我們可以看到這個DataFrame也被導(dǎo)出為JSON文件。
以上就是Pandas實(shí)現(xiàn)解析JSON數(shù)據(jù)與導(dǎo)出的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Pandas解析JSON數(shù)據(jù)的資料請關(guān)注腳本之家其它相關(guān)文章!
- Python基于pandas實(shí)現(xiàn)json格式轉(zhuǎn)換成dataframe的方法
- 對pandas處理json數(shù)據(jù)的方法詳解
- Pandas讀存JSON數(shù)據(jù)操作示例詳解
- 讀Json文件生成pandas數(shù)據(jù)框詳情
- python使用pandas讀取json文件并進(jìn)行刷選導(dǎo)出xlsx文件的方法示例
- Python?Pandas實(shí)現(xiàn)將嵌套JSON數(shù)據(jù)轉(zhuǎn)換DataFrame
- pandas讀取HTML和JSON數(shù)據(jù)的實(shí)現(xiàn)示例
- Python使用pandas讀取Excel并選取列轉(zhuǎn)json
- Pandas JSON的處理使用
相關(guān)文章
Python實(shí)現(xiàn)的隨機(jī)森林算法與簡單總結(jié)
這篇文章主要介紹了Python實(shí)現(xiàn)的隨機(jī)森林算法,結(jié)合實(shí)例形式詳細(xì)分析了隨機(jī)森林算法的概念、原理、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2018-01-01python使用matplotlib畫出的圖怎樣放到word中
這篇文章主要介紹了python使用matplotlib畫出的圖怎樣放到word中問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09python使用wmi模塊獲取windows下硬盤信息的方法
這篇文章主要介紹了python使用wmi模塊獲取windows下硬盤信息的方法,涉及Python獲取系統(tǒng)硬件信息的相關(guān)技巧,需要的朋友可以參考下2015-05-05python計(jì)算程序開始到程序結(jié)束的運(yùn)行時間和程序運(yùn)行的CPU時間
這篇文章主要介紹了python計(jì)算程序開始到程序結(jié)束的運(yùn)行時間和程序運(yùn)行的CPU時間的三個方法,大家參考使用2013-11-11tensorflow實(shí)現(xiàn)打印ckpt模型保存下的變量名稱及變量值
今天小編就為大家分享一篇tensorflow實(shí)現(xiàn)打印ckpt模型保存下的變量名稱及變量值,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01