[英]Pandas: Dataframe from the 2nd level of a dictionary
I have a json file like so:我有一个像这样的json文件:
dict = {'2020-04-13TVL 0620M-Su 6p-12m': {'syscode': '0620',
'weekStartDate': '2020-04-13'},
'2020-04-20HSTE0620M-Su 6p-12m': {'syscode': '0620',
'weekStartDate': '2020-04-20'},
'2020-06-15MTV 6032M-Su 12m-2a': {'syscode': '6032',
'weekStartDate': '2020-06-15'},
'2020-06-15VH1 1572M-Su 12m-2a': {'syscode': '1572',
'weekStartDate': '2020-06-15'}}
How do I avoid the top level keys of the dictionary so I can get a dataframe like so:如何避免字典的顶级键,以便我可以获得如下数据框:
dict = [
{'syscode': '0620', 'weekStartDate': '2020-04-13'},
{'syscode': '0620', 'weekStartDate': '2020-04-20'},
{'syscode': '6032', 'weekStartDate': '2020-06-15'},
{'syscode': '1572', 'weekStartDate': '2020-06-15'}
]
df = pd.DataFrame(dict)
df
syscode weekStartDate
0 0620 2020-04-13
1 0620 2020-04-20
2 6032 2020-06-15
3 1572 2020-06-15
Construct dataframe on dictionary values.在字典值上构建数据框。
Note : d1
is your dict .注意:
d1
是你的字典。 Don't use keyword dict
as variable name.不要使用关键字
dict
作为变量名。 It will overwrite the python keyword dict
.它将覆盖 python 关键字
dict
。
df = pd.DataFrame(d1.values())
Out[25]:
syscode weekStartDate
0 0620 2020-04-13
1 0620 2020-04-20
2 6032 2020-06-15
3 1572 2020-06-15
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.