这是将 JSON 数据集加载到 Pandas DataFrames 中的最有效方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16444797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:48:48  来源:igfitidea点击:

which is the most efficient way of loading a JSON dataset into Pandas DataFrames

pythonjsondataframepandas

提问by Luchux

I didn't find anything in the Pandas documentations and cookbook (just references to CSV, and text files with separators) on JSON.

我在 Pandas 文档和食谱中没有找到任何关于 JSON 的内容(只是对 CSV 的引用,以及带有分隔符的文本文件)。

Is there an already defined function to load JSON directly into DataFrame? If there are different alternatives, which is the most efficient?

是否有一个已经定义的函数可以将 JSON 直接加载到 DataFrame 中?如果有不同的选择,哪个最有效?

回答by erantdo

with pandas 0.12:

使用Pandas 0.12:

import pandas as pd

d = pd.read_json('JSON File')

回答by arpiagar

The generic way to laod JSON to DataFrame is mentioned above:

上面提到了将 JSON 加载到 DataFrame 的通用方法:

import pandas as pd
d = pd.read_json('JSON File')

However, if your JSON file is nested and you need to create DataFrame of some nested attribute in it, one can use

但是,如果您的 JSON 文件是嵌套的并且您需要在其中创建某个嵌套属性的 DataFrame,则可以使用

 from pandas.io.json import json_normalize
 json_normalize(df[JSONKEYWORD])

In the JSONKEYWORD one can pass the nested JSON object and you get a sub Data Frame for that Nested JSON object.

在 JSONKEYWORD 中,可以传递嵌套的 JSON 对象,并且您会获得该嵌套 JSON 对象的子数据帧。

回答by James

Install pandasjson from github which provides DataFrame from_json and to_json classmethods.

从提供 DataFrame from_json 和 to_json 类方法的 github 安装 pandasjson。

https://github.com/pydata/pandasjson

https://github.com/pydata/pandasjson

import pandasjson
from pandas import DataFrame

"""
pinfo DataFrame.from_json
File:  ../lib/python2.7/site-packages/pandasjson.py
Definition: DataFrame.from_json(cls, json, orient='columns', dtype=None, numpy=True)

pinfo DataFrame.to_json
File:  ../lib/python2.7/site-packages/pandasjson.py
Definition: DataFrame.to_json(self, orient='columns', double_precision=10, force_ascii=True)
"""