将具有嵌套字典的字典转换为熊猫数据框

时间:2018-06-21 10:18:20

标签: python pandas

这是的扩展 this question on pandas conversion from list of dicts

如果数据框在字典中包含一个字典

[{'points': 50, 'time': '5:00', 'year': 2010}, 
{'points': 25, 'time': '6:00', 'month': "february"}, 
{'points':90, 'time': '9:00', 'month': 'january'}, 
{'points_h1':20, 'details': {'width':10,'height:20}}]

所以我想得到这个:

      month  points  points_h1  time  year  details.width  details.height
0       NaN      50        NaN  5:00  2010            NaN             NaN
1  february      25        NaN  6:00   NaN            NaN             NaN
2   january      90        NaN  9:00   NaN            NaN             NaN
3       NaN     NaN         20   NaN   NaN             10              20

您可能已经猜到我以前使用过json_normalize,现在有一个dict,但是在这里转换为json对我来说似乎并不直观。在找到上面的答案几乎可以解决我的问题(将专栏转换为list-comprehension并使用pd.concat())之前,我已经尝试过listpd.DataFrame(data)

0 个答案:

没有答案