Python 从有嵌套字典的字典
我有一本这样的字典:Python 从有嵌套字典的字典,python,python-3.x,pandas,dictionary,Python,Python 3.x,Pandas,Dictionary,我有一本这样的字典: {12: {'Soccer': {'value': 31, 'year': 2013}}, 23: {'Volley': {'value': 24, 'year': 2012},'Yoga': {'value': 3, 'year': 2014}}, 39: {'Baseball': {'value': 2, 'year': 2014},'basket': {'value': 4, 'year': 2012}}} 我想要一个这样的数据帧: index
{12: {'Soccer': {'value': 31, 'year': 2013}},
23: {'Volley': {'value': 24, 'year': 2012},'Yoga': {'value': 3, 'year': 2014}},
39: {'Baseball': {'value': 2, 'year': 2014},'basket': {'value': 4, 'year': 2012}}}
我想要一个这样的数据帧:
index column
12 {'Soccer': {'value': 31, 'year': 2013}}
23 {'Volley': {'value': 24, 'year': 2012},'Yoga': {'value': 3, 'year': 2014}}
39 {'Baseball': {'value': 2, 'year': 2014},'basket': {'value': 4, 'year': 2012}}
将每个嵌套字典设置在唯一的列中,行由外部字典的键给定。当我使用orient参数等于index的“from_dict”时,它认为嵌套字典中的键是列的标签,它生成一个方形数据框而不是一个列。。。
非常感谢使用:
df = pd.DataFrame({'column':d})
或:
抱歉,伙计们,我意识到这其实很简单:s=pd.Series(dic,index=dic.keys())如果我的答案或其他答案有帮助,请不要忘记-单击复选标记(
✓答案旁边的代码>),将其从灰显切换为填充。谢谢
df = pd.Series(d).to_frame('column')
print (df)
column
12 {'Soccer': {'year': 2013, 'value': 31}}
23 {'Volley': {'year': 2012, 'value': 24}, 'Yoga'...
39 {'Baseball': {'year': 2014, 'value': 2}, 'bask...
In [65]: pd.DataFrame(d.values(), index=d.keys(), columns=['column'])
Out[65]:
column
12 ({'Soccer': {'value': 31, 'year': 2013}}, {'Vo...
23 ({'Soccer': {'value': 31, 'year': 2013}}, {'Vo...
39 ({'Soccer': {'value': 31, 'year': 2013}}, {'Vo...