【问题标题】:Convert specific dictionary of dictionary into pandas dataframe - pandas [duplicate]将字典的特定字典转换为熊猫数据框 - 熊猫 [重复]
【发布时间】:2020-12-01 12:06:28
【问题描述】:

我有如下所示的字典

d1:

{'teachers': 49, 
      'students': 289,  
      'R': 3.7, 
      'holidays': 165, 
      'Em': {'from': '2020-02-29T20:00:00.000Z', 'to': '2020-03-20T20:00:00.000Z', 
                  'F': 3, 'C': 2},
      'OS':18 
      'sC': {'from': '2020-03-31T20:00:00.000Z', 'to': '2020-05-29T20:00:00.000Z', 
                  'F': 25, 'C': 31}}

我想将上面的字典转换为数据框,如下图所示。

teachers  students R   holidays  Em_from     Em_to       Exam_F  Exam_C  OS  sC_from         sC_to     sC_F   sC_C
49        289      3.7 165       2020-02-29  2020-03-20  3       2       18  2020-03-31  2020-05-29    25     31

【问题讨论】:

    标签: python-3.x pandas dataframe dictionary


    【解决方案1】:

    您可能正在寻找pandas.json_normalize

    d = {'teachers': 49,
          'students': 289,
          'R': 3.7,
          'holidays': 165,
          'Em': {'from': '2020-02-29T20:00:00.000Z', 'to': '2020-03-20T20:00:00.000Z',
                      'F': 3, 'C': 2},
          'OS':18,
          'sC': {'from': '2020-03-31T20:00:00.000Z', 'to': '2020-05-29T20:00:00.000Z',
                      'F': 25, 'C': 31}}
    
    
    print(pd.json_normalize(d, sep='_'))
    

    打印:

       teachers  students    R  ...                     sC_to  sC_F sC_C
    0        49       289  3.7  ...  2020-05-29T20:00:00.000Z    25   31
    
    [1 rows x 13 columns]
    

    【讨论】:

    猜你喜欢
    • 2019-07-18
    • 2018-07-14
    • 2019-05-07
    • 2020-12-01
    • 2020-09-08
    • 2017-11-22
    • 1970-01-01
    • 2022-12-05
    相关资源
    最近更新 更多