如何使用json_normalize扁平化嵌套Json数据 [英] How to flatten nest Json data with json_normalize
问题描述
我正在尝试通过json_normalize将JSON数据导入Dataframe,但无法使其正常工作.
I'm trying to import JSON data to Dataframe via json_normalize but cannot get it to work.
我的数据:
a 键与 c1 键
[
{
"a": "A1",
"b": "B1",
"c": [
{
"c1": "C111",
"c2": "C121",
"c3": ["C1131","C1132"]
}
]
},
{
"a": "A2",
"b": "B2",
"c": [
{
"c1": "C211",
"c2": "C212",
"c3": ["C2131","C2132"]
},
{
"c1": "C221",
"c2": "C222",
"c3": ["C2231"]
}
]
}
]
我想制作一个像这样的DataFrame
I want to make a DataFrame like
a c1(a) c2 c3
0 A1 C111 C121 ["C1131","C1132"]
1 A2 C211 C212 ["C2131","C2132"]
2 A2 C221 C222 ["C2231"]
当我使用json_normalize时,它显示ValueError
:
When I use json_normalize it shows ValueError
:
entity_df = json_normalize(data, 'c', 'a')
ValueError: Conflicting metadata name a, need distinguishing prefix
如何更改json_normalize参数? 任何帮助将不胜感激.
How should I change the json_normalize parameters? Any help will be appreciated.
推荐答案
如果您已经经历过使真实数据变得模糊不清的痛苦,请使模拟数据也具有与真实数据相同的功能.
If you have already gone through the pain of obscuring your real data, make the mock data having the same features as the real one as well.
假设您具有以下JSON:
Assuming you have this JSON:
json_data = [
{
"a": "A1",
"b": "B1",
"c": [
{
"a": "C111",
"c2": "C121",
"c3": ["C1131","C1132"]
}
]
},
{
"a": "A2",
"b": "B2",
"c": [
{
"a": "C211",
"c2": "C212",
"c3": ["C2131","C2132"]
},
{
"a": "C221",
"c2": "C222",
"c3": ["C2231"]
}
]
}
]
您只需要一行代码即可提取它:
You only need one line of code to extract it:
pd.io.json.json_normalize(json_data, 'c', ['a', 'b'], record_prefix='data.')
结果:
data.a data.c2 data.c3 a b
0 C111 C121 [C1131, C1132] A1 B1
1 C211 C212 [C2131, C2132] A2 B2
2 C221 C222 [C2231] A2 B2
record_prefix='data.'
是ValueError: Conflicting metadata name a, need distinguishing prefix
错误消息的含义.
这篇关于如何使用json_normalize扁平化嵌套Json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!