最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Pandas實(shí)現(xiàn)解析JSON數(shù)據(jù)與導(dǎo)出的示例詳解

 更新時(shí)間:2023年07月18日 11:30:08   作者:吃肉的小饅頭  
其實(shí)使用pandas解析JSON?Dataset要方便得多,所以這篇文章主要為大家介紹了Pandas實(shí)現(xiàn)解析JSON數(shù)據(jù)與導(dǎo)出的具體方法,需要的小伙伴可以收藏一下

使用pandas解析JSON Dataset要方便得多。Pandas允許您將列表的列表轉(zhuǎn)換為Dataframe并單獨(dú)指定列名。JSON解析器將JSON文本轉(zhuǎn)換為另一種表示必須接受符合JSON語(yǔ)法的所有文本。它可以接受非JSON形式或擴(kuò)展。實(shí)現(xiàn)可以設(shè)置以下內(nèi)容:

  • 它接受的文本大小的限制,
  • 對(duì)嵌套的最大深度的限制,
  • 對(duì)數(shù)字范圍和精度的限制,
  • 設(shè)置字符串的長(zhǎng)度和字符內(nèi)容的限制。

使用大型JSON數(shù)據(jù)集可能會(huì)惡化,特別是當(dāng)它們太大而無(wú)法容納在內(nèi)存中時(shí)。在這種情況下,命令行工具和Python的組合可以成為探索和分析數(shù)據(jù)的有效方法。

導(dǎo)入JSON文件

JSON的操作是使用Python數(shù)據(jù)分析庫(kù)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對(duì)象到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文件可能非常耗時(shí),并且很難將其展平并加載到Pandas中。

我們使用嵌套的“'raw_nyc_phil.json。"'從一個(gè)嵌套數(shù)組創(chuàng)建一個(gè)扁平化的pandas數(shù)據(jù)框,然后解包一個(gè)深度嵌套數(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)

我們可以看到這個(gè)DataFrame也被導(dǎo)出為JSON文件。

以上就是Pandas實(shí)現(xiàn)解析JSON數(shù)據(jù)與導(dǎo)出的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Pandas解析JSON數(shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • pytorch中可視化之hook鉤子

    pytorch中可視化之hook鉤子

    本文主要介紹了pytorch中可視化之hook鉤子,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Python下利用BeautifulSoup解析HTML的實(shí)現(xiàn)

    Python下利用BeautifulSoup解析HTML的實(shí)現(xiàn)

    這篇文章主要介紹了Python下利用BeautifulSoup解析HTML的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • Pycharm中的Python?Console用法解讀

    Pycharm中的Python?Console用法解讀

    這篇文章主要介紹了Pycharm中的Python?Console用法解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Python中的getattr、__getattr__、__getattribute__、__get__詳解

    Python中的getattr、__getattr__、__getattribute__、__get__詳解

    這篇文章主要為大家介紹了Python中的getattr,__getattr__,__getattribute__和__get__,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-12-12
  • 簡(jiǎn)單了解Python變量作用域正確使用方法

    簡(jiǎn)單了解Python變量作用域正確使用方法

    這篇文章主要介紹了簡(jiǎn)單了解Python變量作用域正確使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Python實(shí)現(xiàn)的簡(jiǎn)單萬(wàn)年歷例子分享

    Python實(shí)現(xiàn)的簡(jiǎn)單萬(wàn)年歷例子分享

    這篇文章主要介紹了Python實(shí)現(xiàn)的簡(jiǎn)單萬(wàn)年歷例子分享,需要的朋友可以參考下
    2014-04-04
  • Python檢測(cè)兩個(gè)文本文件相似性的三種方法

    Python檢測(cè)兩個(gè)文本文件相似性的三種方法

    檢測(cè)兩個(gè)文本文件的相似性是一個(gè)常見(jiàn)的任務(wù),可以用于文本去重、抄襲檢測(cè)等場(chǎng)景,Python 提供了多種方法來(lái)實(shí)現(xiàn)這一功能,x下面小編就來(lái)簡(jiǎn)單介紹一下吧
    2025-03-03
  • Python調(diào)用VBA實(shí)現(xiàn)保留原始樣式的表格合并方法

    Python調(diào)用VBA實(shí)現(xiàn)保留原始樣式的表格合并方法

    本文主要介紹了Python調(diào)用VBA實(shí)現(xiàn)保留原始樣式的表格合并方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • tensorboard 可以顯示graph,卻不能顯示scalar的解決方式

    tensorboard 可以顯示graph,卻不能顯示scalar的解決方式

    今天小編就為大家分享一篇tensorboard 可以顯示graph,卻不能顯示scalar的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • Starship定制shell提示符實(shí)現(xiàn)信息自由

    Starship定制shell提示符實(shí)現(xiàn)信息自由

    這篇文章主要介紹了Starship定制shell提示符的實(shí)現(xiàn),讓你需要的所有信息觸手可及,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03

最新評(píng)論

肥东县| 临潭县| 辽阳县| 陵川县| 克东县| 吉首市| 东安县| 厦门市| 南开区| 芒康县| 京山县| 鸡东县| 昭苏县| 延长县| 涟源市| 庆元县| 尖扎县| 南宁市| 逊克县| 石渠县| 阳山县| 抚松县| 保靖县| 黄大仙区| 北碚区| 太仆寺旗| 渝中区| 乌兰察布市| 南漳县| 洛浦县| 金山区| 邛崃市| 白城市| 卢氏县| 甘洛县| 基隆市| 榆社县| 安远县| 仁怀市| 琼结县| 曲麻莱县|