我的json结构如下:
{
"data": {
"item1": {
"field1": "foo",
"field2": "bar",
"field3": "baz"
},
"item2": {
"field1": "foo1",
"field2": "bar1",
"field3": "baz1"
},
}
}
当我使用pd.io.json.json_normalize
对此数据结构进行规范化时,我得到一个包含1行的数据框,并为每个数据项重复列标题。如何让重复的列显示为行而不是列?
我目前正在接受这个
field1 | field2 | field3 | field1 |field2 | field3
foo | bar | baz | foo1 | bar1 | baz1
我想要的是:
field1 | field2 | field3 |
foo | bar | baz
foo1 | bar1 | baz1
答案 0 :(得分:2)
这就是你想要的吗?
In [31]: d
Out[31]:
{'data': {'item1': {'field1': 'foo', 'field2': 'bar', 'field3': 'baz'},
'item2': {'field1': 'foo1', 'field2': 'bar1', 'field3': 'baz1'}}}
In [32]: pd.DataFrame(d['data']).T
Out[32]:
field1 field2 field3
item1 foo bar baz
item2 foo1 bar1 baz1
答案 1 :(得分:1)
您可以尝试将data
转换为列表,然后使用json_normalize
来规范化数据:
from pandas.io.json import json_normalize
content={"data": {"item1": {"field1": "foo","field2": "bar","field3": "baz"},"item2": {"field1": "foo1","field2": "bar1","field3": "baz1"},}}
content["data"]=content["data"].values()
result = json_normalize(content,"data")
print(result)
输出:
field1 field2 field3
0 foo1 bar1 baz1
1 foo bar baz