Turning my dictionary into a pandas dataframe

后端 未结 4 1146
一生所求
一生所求 2021-01-19 23:01

I have a function which create several dicts of dicts, based on some conditions.

However, I would really like to turn the dict into a dataframe after collecting it.

4条回答
  •  心在旅途
    2021-01-19 23:16

    x = {'TSLA': {2011: {'negative': {'lowPrice': 185.16,
        'lowDate': '05/27/19',
        'highPrice': 365.71,
        'highDate': '12/10/18',
        'change': -0.49}},
      2012: {'negative': {'lowPrice': 185.16,
        'lowDate': '05/27/19',
        'highPrice': 365.71,
        'highDate': '12/10/18',
        'change': -0.49}},
      2013: {'negative': {'lowPrice': 32.91,
        'lowDate': '01/07/13',
        'highPrice': 37.24,
        'highDate': '03/26/12',
        'change': -0.12},
       'positive': {'lowPrice': 32.91,
        'lowDate': '01/07/13',
        'highPrice': 190.9,
        'highDate': '09/23/13',
        'change': 4.8}}}}
    
    y = []
    z = []
    for k0 in x:
        for k1 in x[k0]:
            for k2 in x[k0][k1]:
                y .append((k0, k1, k2))     
                col = x[k0][k1][k2].keys()
                for c in col:
                    z.append(x[k0][k1][k2][c])
    
    
    index = pd.MultiIndex.from_tuples(y)
    df = pd.DataFrame(columns=col, index=index)
    z  = np.array(z).reshape(df.shape)
    df = pd.DataFrame(columns=col, index=index, data=z)
    
    print(df)
    
                       lowPrice   lowDate highPrice  highDate change
    TSLA 2011 negative   185.16  05/27/19    365.71  12/10/18  -0.49
         2012 negative   185.16  05/27/19    365.71  12/10/18  -0.49
         2013 negative    32.91  01/07/13     37.24  03/26/12  -0.12
              positive    32.91  01/07/13     190.9  09/23/13    4.8
    

提交回复
热议问题