- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用亚马逊数据集来回答问题。
亚马逊的代码是:
import pandas as pd
import gzip
def parse(path):
g = gzip.open(path, 'rb')
for l in g:
yield eval(l)
def getDF(path):
i = 0
df = {}
for d in parse(path):
df[i] = d
i += 1
return pd.DataFrame.from_dict(df, orient='index')
df = getDF('QA_Video_Games.json.gz')
df 就像:
df1 = pd.json_normalize(df,record_path=['questions'])
如何正确规范 json 部分,以便我可以将每个部分作为数据框中的单独列获取?
最佳答案
要完全规范化此文件,您可以使用:
from ast import literal_eval
all_data = []
with open("unpacked_file.json", "r") as f_in:
for line in f_in:
all_data.append(literal_eval(line))
df = pd.DataFrame(all_data)
df = df.explode("questions").reset_index(drop=True)
df = (
pd.concat(
[df, pd.json_normalize(df.pop("questions"))],
axis=1,
)
.explode("answers")
.reset_index(drop=True)
)
df = pd.concat(
[df, pd.json_normalize(df.pop("answers"))],
axis=1,
)
print(df)
打印:
asin questionType askerID questionTime questionText answerText answererID answerTime helpful answerType answerScore
0 B0000512IE open-ended A39TQ7XW6P36BU June 29, 2014 Can I use these with Window 8/8.1? Yes, you will need to go to their website to d... AH14F9TYDABNH June 29, 2014 [2, 2] NaN NaN
1 B0000512IE open-ended A39TQ7XW6P36BU June 29, 2014 Can I use these with Window 8/8.1? Sure can. My Windows 8.1 machine automatically... A36BQQD67VJOD2 June 29, 2014 [2, 2] NaN NaN
2 B0000512IE open-ended A39TQ7XW6P36BU June 29, 2014 Can I use these with Window 8/8.1? Mine work with W8, can't answer for W8.1 but s... AZY7TFN31JV8M June 29, 2014 [1, 1] NaN NaN
3 B0000512IE open-ended A39TQ7XW6P36BU June 29, 2014 Can I use these with Window 8/8.1? Yes. I installed on windows 8 and had no problems A1RU4ZCFMLYY9E June 29, 2014 [1, 1] NaN NaN
4 B0000512IE open-ended A39TQ7XW6P36BU June 29, 2014 Can I use these with Window 8/8.1? I don't see why not. I don't use the CH driver... A223O8W9Q7VLAE June 29, 2014 [0, 1] NaN NaN
5 B0000512IE open-ended AL82696CUTIL5 August 27, 2014 Could this be adapted to be used in racing gam... I googled the question and found many forums o... AG8XZT4PKY7UZ August 28, 2014 [0, 0] NaN NaN
6 B0000512IE open-ended AL82696CUTIL5 August 27, 2014 Could this be adapted to be used in racing gam... I've always been under the impression you coul... A261POEFLJDN6L August 28, 2014 [0, 0] NaN NaN
7 B0000512IE open-ended AL82696CUTIL5 August 27, 2014 Could this be adapted to be used in racing gam... Yes I would think so. AHGOG7QAAE3XP August 28, 2014 [0, 0] NaN NaN
...
28890 B00K4S9C9I open-ended A1NJ7DFD2KUOLC March 5, 2015 Would it work on a lap top yes it would work great on a lap top A6JLDUBTXCZA5 March 14, 2015 [0, 0] NaN NaN
28891 B00K4S9C9I yes/no A1X0KO1GKYUXLL January 10, 2015 can u mod this one Yes you can. I personally download from spinti... A215AZEU7D23KI January 10, 2015 [0, 0] Y 0.9897
28892 B00K4S9C9I yes/no A1X0KO1GKYUXLL January 10, 2015 can u mod this one Yes I can ARR5TN6Y7R824 January 10, 2015 [0, 0] Y 0.9807
关于python - 如何正确 json_normalize 以便我以正确的格式获取数据帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67373305/
我正在使用 json_normalize 中给出的示例此处提供的文档pandas.json_normalize — pandas 1.0.3 documentation ,不幸的是,我无法粘贴我的实际
我一直在尝试规范化一个非常嵌套的json文件,稍后我将进行分析。我正在努力解决的是如何进一步深入到正常化。 我浏览了pandas.io.json.json_normalize文档,因为它完全按照我想要
我有一个需要展平的嵌套 JSON 结构。在使用 JSON 规范化时,它会展平所有键。但是,我想在保留嵌套的其他键的同时展平特定键。如何使用 JSON 规范化来实现这一点。我正在尝试做的事情的详细描述如
我有一个没有统一结构的嵌套 json 文件,如下例所示: [{ "name": "Jon", "last": "Jonny"}, {"name": "Jimmy", "last": "johnson
我正在尝试使用 json_normalize 来展平嵌套字典。我的数据是这样的: data = [ {'gra': [ { 'A': 1,
我正在使用亚马逊数据集来回答问题。 亚马逊的代码是: import pandas as pd import gzip def parse(path): g = gzip.open(path, 'r
我正在尝试创建一个 API,从 poloniex 获取历史数据,供我的神经网络用于学校项目的学习。 我得到一个json文件,但问题是poloinex_df是“空”,所有信息都在index/column
我有一个嵌套字典 (json),它是从非官方谷歌字典 API 返回的。 看起来像这样: {'word': 'slack', 'phonetic': '/slak/', 'meaning': {'a
我已经使用以下迭代方法: for k, v in enumerate(my_request['content']): for k1, v1 in enumerate(v['data']['sc
我正在分析一个 json 文件,我想将嵌套的 json 输入文件转换为 python 中的平面数据框。有没有任何 python 方法可以管理这个?或者我应该创建一个自定义函数来做到这一点?您能提供一个
假设这是我的 JSON: ds = [{ "name": "groupa", "subGroups": [{ "subGroup": 1,
我有一个如下所示的数据框: ID phone_numbers 1 [{u'updated_at': u'2017-12-02 15:29:54', u'created_at'
这个问题在这里已经有了答案: Split / Explode a column of dictionaries into separate columns with pandas (13 个答案)
此问题特定于 pandas.DataFrame 中的数据列 这个问题取决于列中的值是否为 str , dict , 或 list类型。 这个问题解决了如何处理 NaN值,当 df.dropna().r
我正在尝试从嵌套的 json 中创建一个 Pandas 数据框。由于某种原因,我似乎无法解决第三个层次。 我的 json 看起来像这样: "numberOfResults": 376, "re
我有一个带有 LISTS(with dicts) 作为列值的数据框。我的目的是规范整列(所有行)。我找到了标准化单行的方法。但是,我无法对整个数据框或列应用相同的功能。 data = {'COLUMN
我目前正在对这个字典文件进行扁平化处理,遇到了一些障碍。我正在尝试使用 json_normalize 来展平这些数据。如果我对单个实例进行测试,它可以工作,但如果我想展平所有数据,它将返回一个错误,指
我目前正在对这个字典文件进行扁平化处理,遇到了一些障碍。我正在尝试使用 json_normalize 来展平这些数据。如果我对单个实例进行测试,它可以工作,但如果我想展平所有数据,它将返回一个错误,指
我想标准化以下 JSON: [ { "studentId": 1, "studentName": "James", "schools": [
我正在尝试在 Python (Pandas) 中使用 json_normalize 来展平 json 文件,但作为菜鸟,我似乎总是以 KeyError 告终。 我想要实现的是一个包含游戏中所有 Pla
我是一名优秀的程序员,十分优秀!