必须将nested字典转换为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中的所有解决方案,但这种问题不是 asked/addressed的任何地方
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
分析解答
您可以查看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