嵌套字典必须转换为pandas df:
columns list = ['city', 'Column_1', 'Column_2',....'Column_20']
{ 'NewYork': {'Column_1': '12146144', 'Column_2': '695',...., 'Column_20': '500'},
'Washington': {'Column_1': '5648', 'Column_2': '864',...., 'Column_20': '734'},
'Dallas': {'Column_1': '876', 'Column_2': '23456', ...., 'Column_20': '9876'},
.....
}
尝试了stackoverflow / google中的所有解决方案,但是这种问题在任何地方都不会被问到/解决
list_of_dict = []
for key, value in nested_dict.items():
for key1, value1 in value.items():
list_of_dict.append({'city':key, key1:value1})
df = pd.DataFrame(list_of_dict)
预期结果:
city Column_1 Column_2 ... Column_20
New York 12146144 695 ... 500
Washington 5648 864 ... 734
Dallas 876 23456 ... 9876
.
.
但是得到的错误结果是:
city Column_1 Column_2 ... Column_20
New York 12146144
New York 695
New York ... 500
....
Washington 5648
Washington 864
Washington ... 734
....
Dallas 876
Dallas 23456
Dallas ... 9876
答案 0 :(得分:2)
您可以使用from_dict
pd.DataFrame.from_dict(d,'index').rename_axis('city').reset_index()
Out[119]:
city Column_20 Column_2 Column_1
0 Dallas 9876 23456 876
1 NewYork 500 695 12146144
2 Washington 734 864 5648