简体   繁体   English

Pandas:来自字典第二级的数据框

[英]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.

相关问题 如何在pandas DataFrame中将二级索引更改为二级列? - How to change 2nd level index into 2nd level column in pandas DataFrame? 选择 Pandas DataFrame 的第二个 MultiIndex Level 作为索引器 - Selecting the 2nd MultiIndex Level of Pandas DataFrame as an Indexer Pandas multindex DataFrame:用第二级其他列的值替换某些值 - Pandas multindex DataFrame: replace certain values by values from other column on 2nd level Python:从 json 提取第二级到 dataframe - Python: Extract 2nd level from json to dataframe 从第二个数据帧有效地映射熊猫中的值 - efficiently mapping values in pandas from a 2nd dataframe 如何强制 pandas dataframe 的第 2 级加起来达到第 1 级? - How to enforce 2nd level of pandas dataframe to add up to 1st level? 合并熊猫中的2个数据框而不合并第2个数据框 - Merge 2 dataframes in Pandas not merging 2nd dataframe 如何根据 Pandas 中第一个数据框中的某些列值从第二个数据框中添加列值 - How to add Column Values from 2nd DataFrame Based on Some Column Values in First dataframe in Pandas 我合并了 Pandas 中的两个数据帧并从中创建了一个数据帧,但它无法对 Pandas 中的第二个数据帧中的值进行排序 - I Merge Two DataFrames in Pandas and Create a Single DataFrame From it but it is not able to sort Values from 2nd Dataframe in Pandas 使用二级索引复制 DataFrame 中的 n 行? - Duplicating n rows in a DataFrame using 2nd level index?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM