gpt4 book ai didi

python - Flatten 函数检索错误的值

转载 作者:行者123 更新时间:2023-12-04 07:54:00 26 4
gpt4 key购买 nike

我目前有一个 Python 脚本,它使用 flatten 函数来展平 JSON 对象,然后我将使用 json_normalize将扁平的 JSON 转换为 DataFrame。

import pandas as pd
from pandas import json_normalize
from collections.abc import MutableMapping as mm

def flatten(dictionary, p_key=None, parent_key=False, separator='.'):
items = []
if isinstance(dictionary, list):
for listval in dictionary:
items.extend(flatten(listval).items())
return dict(items)
for key, value in dictionary.items():
if parent_key:
new_key = f"{str(p_key)}{separator}{key}"
else:
new_key = p_key if p_key else key
if isinstance(value, mm):
items.extend(flatten(
dictionary=value,
p_key=new_key,
parent_key=True,
separator=separator).items())
elif isinstance(value, list):
for k, v in enumerate(value):
items.extend(flatten(
dictionary={str(k): v},
p_key=new_key,
parent_key=False,
separator=separator).items())
else:
items.append((new_key, value))
return dict(items)


jfile = [{
"id" : 1,
"labelId" : 169,
"indicators" : [
62
],
"Wait" : 6,
"Levels" : [
{
"isActive" : "true",
"pressure" : 3,
"actions" : [
{
"isActive" : "true",
"description" : "Place"
}
],
"users" : [
5467,
5469,
5
]
},
{
"isActive" : "true",
"pressure" : 2,
"actions" : [
{
"isActive" : "true",
"description" : "Test"
}
],
"users" : [
3253,
6903
]
}
]
}]


flatdoc = [flatten(i) for i in jfile]

flatdoc = json_normalize(flatdoc)

print(flatdoc)

当前输出:
   id  labelId  indicators  Wait Levels.isActive  Levels.pressure Levels.actions.isActive Levels.actions.description  Levels.users
1 169 62 6 true 2 true Test 6903
由于某种原因,我目前得到的输出是从 JSON 对象中找到的最后一个键中检索最后一个值。我需要它做的是从字典中的第一个键中检索它,然后当该列表没有嵌套并且只包含数字/整数时,它应该简单地分解它,以便结果如下所示:
预期输出:
   id  labelId  indicators  Wait Levels.isActive  Levels.pressure Levels.actions.isActive Levels.actions.description  Levels.users
1 169 62 6 true 3 true Place 5467
1 169 62 6 true 3 true Place 5469
1 169 62 6 true 3 true Place 5
我将如何修改函数以便它找到正确的键以产生适当的值?

最佳答案

您的展平功能是错误的:使用给定的 jfile它只返回一行,而预期会返回 5 行。
这是一个正确的版本:

def flatten(data):
if isinstance(data, (list, tuple)):
newdata = []
for elt in data:
elt = flatten(elt)
if isinstance(elt, list):
newdata.extend(elt)
else:
newdata.append(elt)
return newdata
elif isinstance(data, mm):
for k,v in data.items():
v = flatten(v)
if isinstance(v, list):
newdata = [data.copy() for _ in v]
for i, elt in enumerate(v):
newdata[i][k] = elt
return flatten(newdata)
data[k] = v
return data
然后您可以直接执行以下操作:
flatdoc = pd.json_normalize(flatten(jfile))
获得:
   id  labelId  indicators  Wait Levels.isActive  Levels.pressure Levels.actions.isActive Levels.actions.description  Levels.users
0 1 169 62 6 true 3 true Place 5467
1 1 169 62 6 true 3 true Place 5469
2 1 169 62 6 true 3 true Place 5
3 1 169 62 6 true 2 true Test 3253
4 1 169 62 6 true 2 true Test 6903

关于python - Flatten 函数检索错误的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66796742/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com