JSON
我想将其放入数据框中有些棘手。
{'A': {'name': 'A',
'left_foot': [{'toes': '5'}],
'right_foot': [{'toes': '4'}]},
'B': {'name': 'B',
'left_foot': [{'toes': '3'}],
'right_foot': [{'toes': '5'}]},
...
}
我不需要带有 A 和 B 的第一层,因为它是名称的一部分。永远只有一个 left_foot 和一个 right_foot。
我想要的数据如下:
name left_foot.toes right_foot.toes
0 A 5 4
1 B 3 5
使用这篇文章能够得到脚和脚趾,但如果你说数据[“A”]。有没有更简单的方法?
编辑
我有这样的东西,但我需要"A"
在第一行指定。
df = pd.json_normalize(tickers["A"]).pipe(
lambda x: x.drop('left_foot', 1).join(
x.left_foot.apply(lambda y: pd.Series(merge(y)))
)
).rename(columns={"toes": "left_foot.toes"}).pipe(
lambda x: x.drop('right_foot', 1).join(
x.right_foot.apply(lambda y: pd.Series(merge(y)))
)).rename(columns={"toes": "right_foot.toes"})