Home > OS >  Dictionary to Dataframe with keys as index
Dictionary to Dataframe with keys as index

Time:03-31

Having this dictionary

{'ON':                   time     atm   rr25    bf25   rr10   bf10
 0  2022-03-29 23:00:00  0.0895 -0.008  0.0015 -0.014  0.004,
 '1W':                   time     atm  rr25   bf25   rr10    bf10
 0  2022-03-29 23:00:00  0.0785 -0.01  0.002 -0.017  0.0065,
 '2W':                   time     atm   rr25    bf25   rr10     bf10
 0  2022-03-29 23:00:00  0.0795 -0.011  0.0025 -0.019  0.00837,
 '1M':                   time    atm    rr25    bf25    rr10     bf10
 0  2022-03-29 23:00:00  0.077 -0.0115  0.0025 -0.0207  0.00925,
 '2M':                   time    atm    rr25    bf25    rr10     bf10
 0  2022-03-29 23:00:00  0.075 -0.0115  0.0025 -0.0207  0.00937,
 '3M':                   time      atm   rr25     bf25    rr10     bf10
 0  2022-03-29 23:00:00  0.07325 -0.012  0.00255 -0.0222  0.00969,
 '6M':                   time      atm    rr25     bf25     rr10    bf10
 0  2022-03-29 23:00:00  0.07175 -0.0125  0.00265 -0.02375  0.0102,
 '9M':                   time      atm    rr25     bf25     rr10    bf10
 0  2022-03-29 23:00:00  0.07125 -0.0125  0.00295 -0.02375  0.0115,
 '1Y':                   time    atm    rr25   bf25     rr10    bf10
 0  2022-03-29 23:00:00  0.071 -0.0125  0.003 -0.02381  0.0117,
 '2Y':                   time    atm    rr25    bf25     rr10     bf10
 0  2022-03-29 23:00:00  0.072 -0.0125  0.0032 -0.02375  0.01248}

I would like to convert it to this dataFrame

enter image description here

I tried different approaches by using pd.DataFrame.from_dict but didn't help

CodePudding user response:

Use pd.concat:

df = pd.concat(data).droplevel(1)

Output:

>>> df
                   time     atm   rr25    bf25   rr10    bf10
ON  2022-03-29 23:00:00  0.0895 -0.008  0.0015 -0.014  0.0040
1W  2022-03-29 23:00:00  0.0785 -0.010  0.0020 -0.017  0.0065

Data

>>> data
{'ON':                   time     atm   rr25    bf25   rr10   bf10
 0  2022-03-29 23:00:00  0.0895 -0.008  0.0015 -0.014  0.004,
 '1W':                   time     atm  rr25   bf25   rr10    bf10
 0  2022-03-29 23:00:00  0.0785 -0.01  0.002 -0.017  0.0065}

CodePudding user response:

Could you clarify the format of your input? It does not look like a dictionary to me.

Also, try:

pd.DataFrame(data).T
  • Related