我的输入是一个Python字典(json-like):
d = {
"type": "type1",
"details": {
"name": "foo",
"date": {
"timestamp": "01/02/2023 21:42:44",
"components": {
"day": 2,
"month": 1,
"year": 2023,
"time": "21:42:44"
}
}
},
"infos": {
"records": [
{
"field1": "qux",
"field2": "baz",
}
],
"class": "P"
}
}
字符串
我使用下面的代码:
df = pd.json_normalize(
d,
record_path=["infos", "records"],
meta=[
"type",
["details", "date", "timestamp"],
["details", "date", "components", "year"],
["infos", "class"]
],
errors="ignore"
)
型
这给了我这样的输出:
field1 field2 type details.date.timestamp details.date.components.year infos.class
0 qux baz type1 NaN NaN P
型
但我期待的是这个:
field1 field2 type details.date.timestamp details.date.components.year infos.class
0 qux baz type1 01/02/2023 21:42:44 2023 P
型
老实说,我对meta
参数快疯了!我忽略我做错了什么..
你能解释一下它的逻辑吗?
1条答案
按热度按时间ogq8wdun1#
我认为你应该在
record_path=
中添加额外的[]
:字符串
图纸:
型